Here is the input.txt file that I have.

2 5 -2 3 1 2 4 0
1 2 1 0

These are 2 polynominals. 2x^5 - 2x^3 + x^2 + 4 and x^2 + 1 while <3,5> : 3 is the coefficient, 5 is the degree.

I just can separate 2 lines into 2 files input.txt & input1.txt and read from them.
I want to read the first line to 1 linked list and then the second line to another linked list. Can anyone help me with this?

void readfile(char *filename, List &s, Data &x)
{
	FILE *pfile;
	pfile=fopen(filename,"rt");
	if(pfile==NULL)
	{
		cout<<"Error reading!\n";
		exit(0);
	}
	while(!feof(pfile))
	{
		fscanf(pfile,"%d %d ",&x.coefficient,&x.degree);
		insertHead(s,x);
	}
	fclose(pfile);
}
void main()
{
        List s,s1;
	init(s);
	init(s1);
	Data x;
	readfile("input.txt",s,x);
	readfile("input1.txt",s1,x);

}

Recommended Answers

All 3 Replies

Make

void main()

this:

int main()

Yes, first you need to use C++ fstream, instead of C functions, and incorporate getline() command like so :

ifstream fileInput("input.txt");
if(!fileInput){ cerr << "File not found\n"; }
string line;
while(getline(fileInput,line)){
 cout << line << endl; //prints line by line
}

I also want to put the line into the linked list by insertHead().

Ex: Read 2 5 -2 3 1 2 4 0 from input.txt, insertHead into the linked list 's'
Then read 1 2 1 0 from input.txt again and insert value into another linked list 's1'

How can I apply your method T^T All the number is int, not char.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.