C语言中的运行时错误

C语言中的运行时错误,c,runtime-error,file-handling,c-strings,C,Runtime Error,File Handling,C Strings,当我到达文件中间时,我的代码会给出一个运行时错误。 如果我更改temp2或temp1的值,则它会在文件开头崩溃。 我无法理解我在这个文件中犯的错误 它在一个有100行的小文件上平稳运行 我正在做一个文件搜索项目,所以我需要存储大文件,其中有整个驱动器的目录 #include<stdio.h> #include<string.h> #include<windows.h> #include<conio.h> char file[99999]; void

当我到达文件中间时,我的代码会给出一个运行时错误。 如果我更改temp2或temp1的值,则它会在文件开头崩溃。 我无法理解我在这个文件中犯的错误

它在一个有100行的小文件上平稳运行

我正在做一个文件搜索项目,所以我需要存储大文件,其中有整个驱动器的目录

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<conio.h>
char file[99999];
void brek(char *p, char *q);
void main()
{
    FILE *fp;
    int x = 0, y = 0;
    int a = 0, b = 0;
    int g = 0;
    char temp1[10000];          // temp1 is simply for jumping to the date against the given directory or file
        // the main array storing the lines is temp2.
     char temp2[1000][1000];

    system("chdir C:\\Users\\Faraz\\Documents && dir /s > dir.txt");
    fp = fopen("C:\\Users\\Faraz\\Documents\\dir.txt", "r");

    while ((y = getc(fp)) != EOF)
    {
        file[x] = y;
        x++;
    }
    fclose(fp);
    file[x] = '\0';
    puts(&file[0]);

    // <----the copying of the file to the string "file (globally declared)"is
    // done---->//

    getche();
    system("cls");

    // <-------------------start loop-------------------->//

    a = 0;
    while (file[a] != '\0')     // <-------starting of the loop
    {

        while (file[a] != '/')
        {
            temp1[a] = file[a];
            a++;
        }
        temp1[a] = '\0';

        a = a - 2;
        b = 0;
        while (file[a] != '\n')
        {
            temp2[g][b] = file[a];
            b++;
            a++;
        }
        temp2[g][b] = '\0';
        puts(&temp2[g][0]);

        g++;

    }
    // <-----------------end loop---------------------->//

}
#包括
#包括
#包括
#包括
字符文件[99999];
void-brek(char*p,char*q);
void main()
{
文件*fp;
int x=0,y=0;
int a=0,b=0;
int g=0;
char temp1[10000];//temp1仅用于跳转到给定目录或文件的日期
//存储行的主阵列是temp2。
字符temp2[1000][1000];
系统(“chdir C:\\Users\\Faraz\\Documents&&dir/s>dir.txt”);
fp=fopen(“C:\\Users\\Faraz\\Documents\\dir.txt”、“r”);
而((y=getc(fp))!=EOF)
{
文件[x]=y;
x++;
}
fclose(fp);
文件[x]='\0';
放置(&文件[0]);
// //
getche();
系统(“cls”);
// //
a=0;
而(文件[a]!='\0')/请尝试此方法

int 
main(int argc, char **argv)
{
    LPWIN32_FIND_DATAA fdFile;
    HANDLE hFind = NULL;
    const char *sPath = "C:\\Users\\Faraz\\Documents\\*.*";

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
    {
        printf("no such directory %s\n", sPath);
        return -1;
    }

    do
    {
        printf("Directory: %s\n", fdFile->cFileName);
    }
    while(FindNextFile(hFind, &fdFile)); //Find the next file.

    FindClose(hFind); //Always, Always, clean things up!

    return 0;
}

我发现的答案是,你根本无法在512MB内存中加载1gb的文件。 因此,我们使用

#define BUFFER_SIZE 512
使用FGET,我们逐行读取文件,因此不会使操作系统分配的内存空间过载


当我们开始使用堆空间时,动态内存分配并不是问题的解决方案。

为什么在系统调用更好的时候使用系统?您需要动态内存分配是的,上面的注释适用,但您的主要问题是,您在主循环中使用变量
a
:它对两个进行索引e> file
temp1
@iharob我是个新手动态内存分配对我有什么帮助?@LeeDanielCrocker删除目录列表中的附加信息有帮助,然后跳到我可以处理文件名或目录的部分。我没有windows机器,因此无法测试代码,请告诉我错误我会尽力帮助你。@Zimad现在试试,然后删除错误注释。它会给出输出没有目录存在。你确定路径正确吗?是
没有目录存在
还是
没有这样的目录
?很抱歉,我的错,没有这样的目录,是的,路径正确