Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

reading a line at a time

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok even though there are alot of ifstream related Thread i didnt see any that delt with this problem (i saw word by word reading but not line by line) ok heres what i want to do i have a array of strings and i want to imput a line into each of them using a for loop i know i can get a line at a time but i dont know how to get a spacific line (for example line 34) thx for your help[smile]

Share this post


Link to post
Share on other sites
Advertisement
Here's very rough idea that may help you out:

If you are inputing a specific number of characters to a line.
Say this number is 24.
Are you ending it with a '\n' (newline) character?
If say you want line 34.
File seek until you hit the 33rd '\n' character,
then continue on the next line?

Very rough but if you posted code we could probably see
how you're doing it at the moment.

Share this post


Link to post
Share on other sites
ok yeah heres the code

(are you saying that in the .txt file i should end every line with a \n or is this in the source


#include <stdlib.h> // Include this to use system()
#include <iostream> // Include our standard header
#include <string> // Include this to use strings
#include <fstream>
using namespace std;
int main()
{
cout<< "make file?";
const char *szMakeFile; // file to read from
cin >> szMakeFile
ifstream fin;
string A[64] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
fin.open(szMakeFile)
//read each line of file and store each line in a section of A[64] some how
};


Share this post


Link to post
Share on other sites
it would be in ur text file
have both a MAX LENGTH of each line and a terminator character
Most people generally use the \n or \0 characters
The terminator is used when the line is shorter then MAX LENGTH
other then that you always know each line can be MAX LENGTH

I'll review ur code u just posted...

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
ok yeah heres the code

(are you saying that in the .txt file i should end every line with a \n or is this in the source

*** Source Snippet Removed ***


There's quite a few things in that code-snippet that scream "I need education"

Here, try this:


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main(int argc,char* argv[])
{
// Get the filename to make
cout << "Make File? " << endl;
string sMakeFile;
cin >> sMakeFile; // alternatively, you could use: getline(cin,sMakeFile);

// open up the file and setup space for storage
vector<string> vLines;
ifstream fin(sMakeFile);
if(!fin)
{
cerr << "Error Opening file \"" << sMakeFile << "\"!" << endl;
return 1;
}
string line;
while(fin)
{
getline(fin,line);
vLines.push_back(line);
}

// Now you have a std::vector<std::string> containing all the lines of the text file.
// You can access them just like any array
// e.g: string& line34 = vLines[34];

return 0;
}




If there's anything in there you don't quite get, feel free to ask questions. This forum is fairly helpful.

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
...i know i can get a line at a time but i dont know how to get a spacific line (for example line 34)
It's not possible in a clean fashion. Read all lines into a temporary structure and then retrieve lines from that structure efficiently. Try this:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main()
{
using namespace std;

fstream fin("<file_name>");
vector<string> temp_file;
copy(fin.begin(), fin.end(), back_inserter(temp_file));

// now access line N as temp_file[N]

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main()
{
using namespace std;

fstream fin("<file_name>");
vector<string> temp_file;
copy(fin.begin(), fin.end(), back_inserter(temp_file));

// now access line N as temp_file[N]

return 0;
}



There is a slight mistake in that code, I/O stream's have no begin()/end() methods, you'll need to use the stream iterators when working with STL algorithms:


#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>

int main() {

using namespace std;

ifstream in("<file_name>");
vector<string> temp_file;

copy(istream_iterator<string>(in),
istream_iterator<string>(),
back_inserter(temp_file));

copy(temp_file.begin(), temp_file.end(),
ostream_iterator<string>(cout, "\n"));

return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!