for loop to read text file

Hi All,

I want a for loop to loop thru each line of a text file.

I want this in C programming not in C++. Because the libs objs i am using in my project are not allowing me to use getline function in C++. evrytime I use that function, it throws a linking error lnk2005 already defined in one of the obj file which i am using in my project.

So please help me.

My input file will be something like this.
1
2
3
4
 Name|Age|Sex|Address
szpt9m|28|Male|Bangalore
ABC|20|female|India
XYZ|56|Male|Engalnd


My Program should read the input file as below.

First it should take the first line then loop thru all other line. Then take the 2nd line and loop thru all other lines below that and so.

So I was thinking of having two for loops one inside the other.

like
1
2
3
4
5
6
7
8
for(loop thru the lines of text file)
{
    get the firstline. and do your work on that
    for(loop thru the lines below that line)
    {
        get the 2nd/3rd/etc line. do your work on that
    }
}


I am a beginner in programming. So please help me.

Thank you
i thought of using the below code but it always returns bad poinetr when i try to assign char array to a string variable. :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 int main(int argc, char* argv[])
{

	int iLineNo = 0;

	size_t len;
	
	char   line[2000]       = " ";


	FILE * fp_input_file					= NULL;

	string temp("");

	vector<string> tempVector;

	fp_input_file=fopen("C:\\test.txt","r");

	
	fgets(line, sizeof(line), fp_input_file);

	while(fgets(line, sizeof(line), fp_input_file)!=NULL)
	{ 
		line[iman_strlen(line)-1]='\0';

		temp = line;

		tempVector.push_back(temp);
		
	}
	

	return 0;
}


when i assign line value to temp it returns a bad pointer there.. please help me where i am missing?

Thank you
Why would you use a strlen function, which requires a terminating nul character to determine the length of a string to decide where to place a terminating nul character to end a string? Why are you using C file functions? Why are you using a character array instead of a string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

void print(const std::vector<std::string>& v)
{
    for ( unsigned i=0; i<v.size(); ++i )
        std::cout << i+1 << ": " << v[i] << '\n' ;
}

int main()
{
    std::string filename("test.txt") ;
    std::ifstream in(filename.c_str()) ;

    if ( !in.is_open() )
    {
        std::cerr << "Unable to open " << filename << '\n' ;
        return 0 ;
    }

    std::vector<std::string> lines ;
    std::string line ;

    while ( std::getline(in, line) )
        lines.push_back(line) ;

    print(lines) ;
}
Last edited on
Hi

Thanks for your reply. As I mentioned in my first post, I cannot use getline function in my program since it conflicts with one of the obj file which i am suing in my program and is a mandatory obj file for my tool to run in the application i am using.

getline throws a linking error lnk2005 which says getline is already defined in xxx.obj file.

So i need a different approach to read the file.
So i need a different approach to read the file.


I suspect what you need to do is to stop plastering using namespace std; all over and use the namespace-qualified identifiers.
Last edited on
i didnt understand that. I am a beginner. could you explain if you dont mind?
He is saying instead of

1
2
using namespace std;
cout << "Hello" << endl;


or whatever, use:

 
std::cout << "Hello" << endl;


It's often frowned upon, except in VERY simple projects,
to use an entire namespace (especially for something so large)...
New people don't often learn this, because the docs teaching them are often using namespace std;
Topic archived. No new replies allowed.