0

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?

2
  • "%49[^;];%d" --> "%49[^;];%d%*c" or " %49[^;];%d" @chux have pointed out about the newline in the previous question. Commented Dec 29, 2014 at 13:48
  • The space skips all \n(newlines) and spaces before scanning data Commented Dec 29, 2014 at 14:29

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm just a begginer so I have some questions. Why is that *c on the fscanf? What is size_t? What is malloc(1 + fileSize);? What is free(fileContent)? The same as rewind? But I'll try something out based on this
@Ran I updated the code, hope it's more helpful now.
I've made mine with the change of a space only and it worked, thank you though, I've learned some new things with this one. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.