Rewrite while loop using for to read a txt file

If I want to use for() loop instead of a while()loop to read a file, do I count a total number of lines in an input file? If so, how to count it? Will it be slower? Also, is it possible to read all lines at once and then process it one by one using multithread?

for example, beginning to rewrite below to be a for loop...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const char* filename = "abc.txt";
FILE* file;
    errno_t OK = fopen_s(&file, filename, "r"); 
    if (open != 0){
        fprintf(stderr, "Error: failed to open file %s\n", filename);
        return 1;}

char buffer[10000];

    while (!feof(file))
    {
        fgets(buffer, 10000, file); // skip first line
        fgets(buffer, 10000, file);
        int n = (int)strlen(buffer) - 1;
        buffer[n] = 0;
        partition(buffer, n); //link to process file
    }
    fclose(file);

Why do you want a for loop?

1
2
3
4
5
6
7
for (; fgets(buffer, 10000, file; ) {
    if (fgets(buffer, 10000, file) {
        const size_t n = strlen(buffer) - 1;
        buff[n] = 0;
        partition(buffer, n)
    }
}


is it possible to read all lines at once


As in using multi-threads to read each line? Only if each line is the exact same size. Otherwise, once you have read the file contents into say dynamic memory, then each line can be processed using threads. How many lines are we talking about?

On Windows, you can read the whole of a file in fixed sized 'chunks', with each 'chunk' being read using threads. The whole file is then assembled into memory for use as required. (Linux ???) Once assembled, then each line can be processed using threads.
Hello Sandra710,

Some tips:

It is better to post enough code that can be compiled and tested. Also there might be something elsewhere in the program that may be causing a problem that you do not see yet.

Your program reads an input file. Please post the input file or at least a fair sample that can be used to test the program. When everyone is using the same information it helps.

To understand using the tags better http://www.cplusplus.com/articles/z13hAqkS/

Your while loop:
1
2
3
4
5
6
7
8
9
10
11
12
while (!feof(file))
{
    fgets(buffer, 10000, file); // skip first line

    fgets(buffer, 10000, file);

    int n = (int)strlen(buffer) - 1;

    buffer[n] = 0;

    partition(buffer, n); //link to process file
}

while (!feof(file)) does not work the way that you are thinking. By the time the while condition becomes false you have entered the loop 1 to many times.

I also have to question line 3. Does this need to be done every time you enter the while loop? Or as the comment suggests that it should be done 1 before the while loop. Need an answer before anyone takes to guessing at what you really need to do.

Andy

P.S. Is this a C program or a C++ program. If this is a C program mention that early in your post so you do not receive answers in C++ code.
Last edited on
1
2
        int n = (int)strlen(buffer) - 1;
        buffer[n] = 0;

The problem with this code is that it will fail if the line was bigger than 10000 (unlikely) and if the files doesn't end with a newline (more likely). So really, you have to check if buffer's length is non-zero and if the last character is a newline.

You can avoid all these problems by using std::getline() instead.
Topic archived. No new replies allowed.