Opening sequential text files with Fstream

Hi I currently have a list of text files that are in order, text01, text02, text03 etc etc.. the list is always different day to day but they are rewritten in the same directory, so somedays there are 5-10 text files and another day up to 300 text files or more (point being I am unsure of the limit)

I open the text file, grab the first 4 lines from it and store that in a string array named Line[]. with the following code:

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
//Main Function
int main ()
{
	//Opening Text file for reading
	ifstream textFile;
	textFile.open ("text01.txt") ;
	
	//Close Program if unable to open Text File
	if (! textFile.is_open () )
	{
		cout << "Unable to open file" << endl;
		return 0;
	}

	//Array for the lines of text
	string Line[4];
	//Loop each line of text and then store it in the Line array
	for (int i = 0; i < 4; i++)
	{
		getline (textFile, Line[i]);	
	}

	//Close File now we are done with it
	textFile.close();
}


I am fairly new to c++ so this is the best solution I could come up with to grab the 4 lines and store them (is this the most efficient way?)

But efficiency aside I want to be able to open the next sequential text file which will be text02 (without writing the file names individually I want c++ to follow the naming convention)

Once I figure out how to do that (your help please!) would I then turn Line[4] into a 2d array? Line[4][?] (? is because i dont know how many files It would be) so my assumption would be that it works something like this:

Line[4]contains 4 lines collected from the text file, and the second bracket of the 2d array would contain which text file is stored so if I wanted the second line of text from text01 I would use:

Line[1][0];

If I wanted the second line of text from text02 I would use:

Line[1][1];

This may be completely incorrect I am kind of just trying to make an educated guess and I have not got to the vectors chapter yet so any insight would be really appreciated!

Cheers,

Ben.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	int i = 0;
	std::stringstream buffer; // a holding space for i to become a string
    std::string str,filename; // Two string values holding the filename and i
	while(i <10)//change to 'false' it will loop forever
    {
    i++;
    buffer.str(""); //clearing out the buffer, explained below
    buffer << i; //Put i in the buffer
    str = buffer.str(); // return the buffer as a string to 'str'
    filename = "text"+str+".txt"; // joining 3 strings and putting it into filename
    };


This should work just use 'filename' in the brackets like this : textFile.open(filename)

The reason we empty the buffer before each time we convert i to a string is because the file names would become this:

text1.txt text12.txt text123.txt ect

The values in the buffer from the previous conversion would still be there.
Last edited on
that looks cool I will test it out when I get home tonight , if that all goes well for opening the files is using a 2day array the correct way of storing the 4 first lines of text and obv each text file will just be the next element in the second bracket or is there a better way to store the strings I need?
"Incomplete type is not allowed" red underscore in the compiler under "buffer" on line 2
Post your updated code please.
The updated code is just copying in Night of Nights post at the start of my main function and the second line instantly gave the error "Incomplete type is not allowed" I commented out all code after it and just put that in as the main function because I was gunna play around with some couts and filenames etc.
Try to include sstream
 
#include <sstream> 


I'm not sure whats the problem if that doesn't work
Last edited on
Worked great, thanks alot :)
Topic archived. No new replies allowed.