Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hy I have a text file . in this file there are 3 text liens .
I want to read the text file .
How can I read line by line this file ?
Updated 21-Sep-12 21:16pm
[no name] 23-Sep-12 10:49am    
It is really embarrassing to ask these kind of questions.

If you want to keep it simple stupid:
#include <stdio.h>

void process_line(const char* line)
    // you line processing here

void test()
    FILE* f = fopen("myfile.txt", "r");
    if (f)
        // replace the line below to "for (;;)" if you want to process all lines
        for (int i=0; i<3; ++i)
            const int MAX_LINE_LENGTH = 0x100;
            char line[MAX_LINE_LENGTH];
            if (!fgets(line, sizeof(line), f))
            int len = strlen(line);
            if (len>0 && line[len-1]=='\n')
                line[len-1] = 0;
Share this answer
Richard MacCutchan 22-Sep-12 8:56am    
Here's the +5 I owe you :)
pasztorpisti 22-Sep-12 9:10am    
Thank you, its even one more than needed. I will remember it! :-)
Richard MacCutchan 22-Sep-12 9:14am    
It was in response to your comment about "How to inject a DLL into a .exe".
pasztorpisti 22-Sep-12 9:16am    
Got it! ;-)
You could use the C library fread()[^] function, or the C++ iostream[^] class.
Share this answer
pasztorpisti 22-Sep-12 9:15am    
+5, iostream can also be used if someone isnt allergic to its syntax.
Richard MacCutchan 22-Sep-12 10:04am    
It usually takes me an hour or two to figure it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900