Click here to Skip to main content
13,091,300 members (49,586 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 ?
Posted 21-Sep-12 21:15pm
Updated 21-Sep-12 21:16pm
Meysam Tolouee 23-Sep-12 10:49am
It is really embarrassing to ask these kind of questions.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You could use the C library fread()[^] function, or the C++ iostream[^] class.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100