【问题标题】:Reading content from multiple files in C从C中的多个文件中读取内容
【发布时间】:2014-12-29 13:41:01
【问题描述】:

例子:

三个文件

hi.txt

txt 里面:“愿我们成为”

再次.txt

txt里面:“曾经的那些人”

final.txt

txt 内:“知道 C”

然后,另一个名为“order”的文件

order.txt

txt里面:

"hi.txt;6"

"again.txt;7"

"final.txt;3"

我想要什么:读取第一个文件名,打开它,列出内容,等待 6 秒,读取第二个文件名,打开它,列出内容,等待 7 秒,读取第三个文件名,打开它,列出内容,请等待 3 秒。

如果我在不打开内容的情况下执行此操作(您会在我的代码上看到一秒钟)并列出名称,它可以工作,但由于某种原因,当涉及内容时它不会。

orderFile = fopen("order.txt","r");

    while(fscanf(orderFile,"%49[^;];%d",fileName,&seconds) == 2)
    {
        contentFile = fopen(fileName,"r");

        while(fscanf(contentFile,"%[^\t]",textContent) == 1)
        {
            printf("%s\n", textContent);
        }

        sleep(seconds);

        fclose(contentFile);
    }

fclose(orderFile);

输出:

愿我们成为

(等待 7 秒)

程序以“RUN SUCCESSFUL”结束

EDIT@

正如你们所说,它现在可以工作了,这就是问题所在:

旧:

while(fscanf(orderFile,"%49[^;];%d",fileName,&seconds) == 2)

新功能:

while(fscanf(orderFile," %49[^;];%d",fileName,&seconds) == 2)

我很难完全理解它,空间有什么作用?不接受输入?空间?究竟是什么?

【问题讨论】:

  • "%49[^;];%d" --> "%49[^;];%d%*c" or " %49[^;];%d" @chux 已经在上一个问题中指出了换行符。
  • 空格在扫描数据前跳过所有\n(换行符)和空格

标签: c file


【解决方案1】:

不要为此使用fscanf

int
main()
{
    FILE *orderFile = fopen("order.txt", "r");
    if (orderFile != NULL)
    {
        int  seconds;
        char line[128];

        /* 
         * fgets, read sizeof line characters or unitl '\n' is encountered
         * this will read one line if it has less than sizeof line characters
         */
        while (fgets(line, sizeof line, orderFile) != NULL)
        {
            /* 
             * size_t is usually unsigned long int, and is a type used 
             * by some standard functions.
             */
            size_t fileSize;
            char  *fileContent;
            FILE  *contentFile;     
            char   fileName[50];
            /* parse the readline with scanf, extract fileName and seconds */
            if (sscanf(line, "%49[^;];%d", fileName, &seconds) != 2)
                continue;
            /* try opening the file */
            contentFile = fopen(fileName,"r");
            if (contentFile == NULL)
                continue;
            /* seek to the end of the file */
            fseek(contentFile, 0, SEEK_END);
            /* 
             * get current position in the stream, 
             * it's the file size, since we are at the end of it 
             */
            fileSize = ftell(contentFile);
            /* seek back to the begining of the stream */
            rewind(contentFile);
            /* 
             * request space in memory to store the file's content
             * if the file turns out to be too large, this call will
             * fail, and you will need a different approach.
             * 
             * Like reading smaller portions of the file in a loop.
             */
            fileContent = malloc(1 + fileSize);
            /* check if the system gave us space */
            if (fileContent != NULL)
            {
                size_t readSize;
                /* read the whole content from the file */
                readSize = fread(fileContent, 1, fileSize, contentFile);
                /* add a null terminator to the string */
                fileContent[readSize] = '\0';
                /* show the contents */
                printf("%s\n", fileContent);
                /* release the memory back to the system */
                free(fileContent);
            }
            sleep(seconds);
            fclose(contentFile);
        }
        fclose(orderFile);  
    }

    return 0;
}

代码中几乎没有解释所有内容,如果您需要更多信息,请阅读手册。

【讨论】:

  • 我只是一个初学者,所以我有一些问题。为什么 fscanf 上是 *c?什么是 size_t?什么是 malloc(1 + fileSize);?什么是免费的(文件内容)?和倒带一样吗?但我会在此基础上尝试一些东西
  • @Ran 我更新了代码,希望现在对你有帮助。
  • 我只改变了一个空间就做了我的,它工作了,不过谢谢你,我已经从这个中学到了一些新东西。 :)
猜你喜欢
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 2015-11-20
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多