This code should be complete but I cant get it what the problem is. It is a very strange thing that happening. I might need a professional eye to see if the problem could be found :)
The problem is this:

The mainthing is that I am using a for loop to go through 5 .txt file paths that I have read in into a vector called: "ReadInGroup" from a .txtfile (NewDataGroup.txt)
The lines in this file NewDataGroup.txt look like this, so this should be correct:

C:\Documents and Settings\Jennifer\Desktop\New Data\Hello1.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello2.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello3.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello4.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello5.txt

Now is the funny this. The while loop reads in the path-lines in the vector as it should do. This works!
(Below I also have the 5 paths between /* */

So the for loop should now loop through one file at a time and read these files and a little futher down in the code I use a while loop to read in all data in these files into a vector.
The for loop goes through the 5 files above though I have used a messagebox to confirm it before.
The funny here is that the for-loop goes through the 4 first files above very fast, then stops at the last file(Hello5.txt) and do read in this files data into a vector. (Why the last file, very strange ? This meens it works in a way)

Now, I have specified manually the filenames/paths below between /* */
(Hello1.txt - Hello5.txt) as a test to see what happens ?
So if you take these /* */ away, this will mean that the already read in vector ReadinGroup will replace with the manually specified paths as I have done below.

If I do this, then it goes through all 5 files and read all 5 files into the vector as it should do.
So the code is correct done but something is missing.

What is the difference, Why does it work when I manually specify the pathway to the files and not when I have read them in, into the vector.
In both cases the for loop will read from the same 5 elements.
I really have no clue on this one.

std::string Dummy;
std::string Dummy2;
std::string FileName1;
std::string FileName2;
std::string FileName3;
			
						
ifstream NewDataGroup2("C:\\Globe\\Append Groups\\NewDataGroup.txt");

std::vector<string> ReadInGroup(10000);
std::string ReadIn;
int countGroup = 0;

	 while( getline(NewDataGroup2, ReadIn, '\n') )
	{
		 countGroup = (countGroup + 1);
		 ReadInGroup[countGroup] = ReadIn;
	 }

/*ReadInGroup[1] = "C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello1.txt";
ReadInGroup[2] = "C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello2.txt";
ReadInGroup[3] = "C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello3.txt";
ReadInGroup[4] = "C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello4.txt";
ReadInGroup[5] = "C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello5.txt";*/
		
			
for (int Starting = 1; Starting < (countGroup + 1); Starting++)
 {		
       std::vector<string> ReadInData;
       ifstream ReadFile(ReadInGroup[Starting].c_str());
       FileName1 = ReadInGroup[Starting].substr(ReadInGroup[Starting].rfind("\\")+1); 

	 FileName2 = Files16.c_str();
	 FileName3 = FileName2 + "\\" + FileName1;     //FileName1 Substr
	 ifstream FindFile( FileName3.c_str() );	         //specify file in path

	 String^ Symbol10 = gcnew String(FileName1.c_str());
				
				 
	 //All Data in this particular .txt File is in memory with this loop !
	 while( getline(FindFile, Dummy2, '\n') )
	 {
		 ReadInData.push_back(Dummy2);
	 }
				
	 //Continue To fill this vector up with other data 
	while( getline(ReadFile, Dummy, '\n') )
	 {
		 ReadInData.push_back(Dummy);
	 }
}	//End Foor Loop

Recommended Answers

All 13 Replies

I realize you didn't post all your program, but what do you do with the vector ReadInData that is declared inside that for loop ? The entire vector is destroyed on each loop iteration and you do nothing with it but toss it into the bit bucket.

Thank you. You are right about I didn´t post all code, there is a part missing at the bottom where I put the vector ReadInData contents to a file.

Yes I do declare: std::vector<string> ReadInData;
in the foor-loop and after putting the contents to the output file, I clear the vector like this: ReadInData.clear();
I dont know if this is okay to do though as I want the ReadInData to be emty after each loop as each loop will read in new data from a new .txt file, so I need it to be emty.
I dont know if this info helps, if you want me to post the entire code, I will do that.
Thank you... !


I realize you didn't post all your program, but what do you do with the vector ReadInData that is declared inside that for loop ? The entire vector is destroyed on each loop iteration and you do nothing with it but toss it into the bit bucket.

You don't have to specifically clear the vector because it will be destroyed and recreated on each loop iteration. Just wasting cpu cycles to clear it yourself.

>>foor-loop
Its spelled for-loop. There is no such word as foor, other than as a proper name. :)


>>What is the difference, Why does it work when I manually specify the pathway to the files
My guess is that the files are not where you expect them to be. After opening a file call the is_open() function to see if the open succeeded. I'll bet it failed. Carefully compare the files and their paths in NewDataGroup.txt with those you hard-coded in the program.

Yes, "for-loop" ofcourse :). I will not clear the vector then. So now I know that it clears itself after each for-loop, thanks for that info.
I am not really sure how to use is_open().
Though I looked really carefully about the path between the hardcoded and what I have in the NewDataGroup.txt.

I tried to change so the ReadInGroup red in the hardcoded pathways and the for-loop went through all 5 five files as it should.
The second line in the example below is the hardcoded one.
The first is the line from the file. I beleive these should be the same.

If I change so it reads in the lines from NewDataGroup.txt it will not work except for the last file. ?
The files are in the right folder, I am 100% of this as the hardcoded example works and the last file from the NewDataGroup.txt.

It confuses me really :) I understand that it is difficult to know where the problem is also.

C:\Documents and Settings\Jennifer\Desktop\New Data\Hello1.txt
"C:\\Documents and Settings\\Jennifer\\Desktop\\New Data\\Hello1.txt";

You don't have to specifically clear the vector because it will be destroyed and recreated on each loop iteration. Just wasting cpu cycles to clear it yourself.

>>foor-loop
Its spelled for-loop. There is no such word as foor, other than as a proper name. :)


>>What is the difference, Why does it work when I manually specify the pathway to the files
My guess is that the files are not where you expect them to be. After opening a file call the is_open() function to see if the open succeeded. I'll bet it failed. Carefully compare the files and their paths in NewDataGroup.txt with those you hard-coded in the program.

Please post the contents of NewDataGroup.txt -- or at least the first 5 or 6 lines if the file is really huge.

This is All contents in NewDataGroup.txt, (there is no more lines than this)
These 5 lines is red in into the ReadInGroup vector wich also works great with the while loop.
(The last line will work in the for-loop but the other lines just goes through with the for-loop very fast without reading in the data from the files.)

C:\Documents and Settings\Jennifer\Desktop\New Data\Hello1.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello2.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello3.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello4.txt
C:\Documents and Settings\Jennifer\Desktop\New Data\Hello5.txt

You posted those in your original post -- I missed that :)

to use is_open()

ifstream FindFile( FileName3.c_str() );
if( FindFile.is_open() == 0)
{
   cout << "Error opening file: \"" << FileName3 << "\"\n";
}

Sorry that I can't be much more help because you didn't post enough of the code to allow me to compile and test.

If you run that code on any other version of Windows other than XP it will not work because "c:\Documents and Setting" doesn't exist on Vista.

Thank you Ancient Dragon, i have XP and I did run the code and received "Error opening file" on the first 4 files.
I will try to just experiment around and see if something can be solved, perheps otherwise I can post a complete code some other time.
Strange though if the files dont open but the last one does.
Thanks for your help :)

You posted those in your original post -- I missed that :)

to use is_open()

ifstream FindFile( FileName3.c_str() );
if( FindFile.is_open() == 0)
{
   cout << "Error opening file: \"" << FileName3 << "\"\n";
}

Sorry that I can't be much more help because you didn't post enough of the code to allow me to compile and test.

If you run that code on any other version of Windows other than XP it will not work because "c:\Documents and Setting" doesn't exist on Vista.

Next step: display the name of the file that it is attempting to open, then compare it with what you think it should be. There is obviously something wrong with it. Even one mis-placed space will make a difference.

Yes, this was actually the first thing I did. I will do this again but more carefully.
I will check out every small detail and see what I will get out of it. Perheps change the filedestinations also etc for testing.
Thank you again!

Next step: display the name of the file that it is attempting to open, then compare it with what you think it should be. There is obviously something wrong with it. Even one mis-placed space will make a difference.

If you look at the code snipped I posted earlier you will see that the filename is surrounded by double quotes to that you can easily tell if there are leading and/or trailing spaces.

A minor note .. why are you skipping the ReadInGroup[] at index 0?
In your for loop, you start at index 1 instead of 0, so that leaves the first
index of ReadInGroup[] unprocessed.
Is that what you want, or should you change

for (int Starting = 1; Starting < (countGroup + 1); Starting++)
 {		
       std::vector<string> ReadInData;
       ifstream ReadFile(ReadInGroup[Starting].c_str());

to

for (int Starting = [B]0[/B]; Starting < countGroup [B]/* + 1*/[/B]; Starting++)
 {		
       std::vector<string> ReadInData;
       ifstream ReadFile(ReadInGroup[Starting].c_str());

I did solve the problem. Though I dont exactly know the difference I have done.
I did not post this code and there for ofcourse impossible to know about the problem.

The thing was that I browsed a directory for .txt files that appeard in a textBox.
From here I just put all contents in this textBox to: textBox1->Text->ToString();

And this I did put to ofstream(OutPutFile <<) and it looked exactly the same with spaces and new lines as this code below that do works now.
So it must be a difference between these. Even that the output look exactly the same.
So the while-loop that after this read from the file, do read in everything correctly.

Thank you...

array<System::String^>^ ReadLines =  gcnew array<System::String^> ( textBox1->Lines->Length ); 
ReadLines = textBox1->Lines;
String^ Group1Out = "c:\\Globe\\Append Groups\\NewDataGroup.txt";
StreamWriter File = gcnew FileStream(Group1Out, FileMode::Create);

for(int count5 = 0; count5 < textBox1->Lines->Length; count5++)
{
        File.WriteLine(ReadLines[count5]);
}

File.Close();
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.