简体   繁体   中英

Reading content from multiple files in C

Example:

Three files

hi.txt

Inside of txt: "May we be"

again.txt

Inside of txt: "The ones who once"

final.txt

Inside of txt: "knew C"

And then, another file called "order"

order.txt

Inside of txt:

" hi.txt;6 "

" again.txt;7 "

" final.txt;3 "

What I want: read the first file name, open it, list the content, wait 6 seconds, read the second name, open it, list the content, wait 7 seconds, read the third name, open it, list the content, wait 3 seconds.

If I do it without opening the content (you'll see a second while on my code) and list the names, it works, yet for some reason it doesn't when it's about the content.

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);

Output:

May we be

(Waits 7 seconds)

Program closes with "RUN SUCCESSFUL"

EDIT@

It works now, as you guys said, this was the problem:

Old:

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

New:

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

I'm having a "hard" time to completely understand it, what does the space does? doesn't accept enters? spaces? What exactly is it?

Don't use fscanf for that

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;
}

Everything is barely explained in the code, read the manuals if you need more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM