help with for / function

Hi there,

I have a function called fillRow(parameter1, parameter2). The first parameter is an int and the second is a string.

I'll try and expalin this as briefly as possible. I want this function to fill rows with data that is read from multiple files. Each file's data is placed in a row. The int is the row number and the string is the pathname of the file. If I manually enter values in (e.g. fillRow(0, pathname) where string pathname = "C:\\test.txt") it works fine. However, I want to 'automate' my program.

So I'm trying to do this using for. This is what I've been trying:
1
2
3
4
ifstream filein("C:\\FileList.txt");
	 for (string line; getline(filein, line);)
		fillRow(0, line);
filein.close()


Now, the file 'FileList.txt' has a file's pathname on each line. Each line (i.e. pathname) is passed into the fillRow() function. The int paramater has been set to zero for now but I need this to change with each line that contains its unique pathname. With this code above, row zero displays the data from the last file pathname that is found in 'FileList.txt'. I believe that this is because every row of data is written over until the last line in 'FileList.txt' is read.

I tried this code but I was unsuccessful:
1
2
3
4
5
ifstream filein("C:\\FileList.txt");
	int j; 
        for (j = 0, string line; j<10, getline(filein, line), j++;)
	     fillRow(j, line);
filein.close()


That is, j is representing line numbers in 'FileList.txt'. This also means that j represents row numbers. I hope this makes sense.

So how could I go about making this idea work? Have I provided you with enough information?

Thanks for reading this post and anu advice / ideas would be great!!
> Have I provided you with enough information?
here's a checklist http://www.eelis.net/iso-c++/testcase.xhtml

> I tried this code but I was unsuccessful:
how is that code not giving you what you want. (crash, wrong output, freeze)

1
2
3
for (j = 0, string line; //compilation error
j<10, getline(filein, line), j++; //comma operator is not a logic operator
)
Let's look at your for loop:
1
2
3
4
for (
j = 0, string line; //Action performed before loop
 j<10, getline(filein, line), j++; //Condition of the loop
) //Action after each iteration. 


Now look at your condition. It uses comma operator which evaluates each operand left to right and returns value of last one. So condition of the loop can be written as: if(j++) 
As j++ returns original value and you have set it at 0 in the begining, your loop will never execute.

Do not use comma operator:
1
2
std::string line;
for (j = 0; j < 10 && getline(filein, line); ++j)
Last edited on
Thanks for the links and suggested code ne555 and MiiNiPaa. Code works very well now.

MiiNiiPaa, thanks for your explanation too. I really appreciated it!

Cheers.
Topic archived. No new replies allowed.