file IO line by line

I have jsut started to learn C++, and I am working on my 5th program. It is a fairly simple "labyrinth" style game. Minus the maze (for now), because I lack graphic abilties. I used an array to mark the dimensions of the room. and with every step, I force the player to solve a problem.

I wrote the program below to write the "list" function for me. All that is left is to add the questions and answers. I need a program/code that will read the line of code I want from "file_A", which has a list from all my Q&A, and write that line in "file_B".

I know how to read from a file. But not how to copy specific lines of text. Do you know where I can find to code that will allow me to do that?

If you can just point me towards a guide/tutorial that can show me how to write a program to do what I need. Or show me a code that I can look at and learn from. That would be awesome.

The way I wrote my game is: The functions that handles the questions returns 3 values.
1- The type of question it is.
2- The question the player needs to solve.
3- The answer to the question.

The guides I have been using only teach the basics of reading and writing to a file. What I use in the program above is as far as it goes. I can write to a file, append to a file, copy over a file, or print the texts of a file. I have no more control than that, atm. I have looked but I havent found anything that does the simple job that I need.

Thank you for your help.

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
26
27
28
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
  int x;
  int y;
  
  ofstream a_file ( "list.cpp", ios::app );

  a_file<<"void list() \n{\n\nstring word2;\n\n";
  
  for ( x = 0; x < 4;x++ ) {
    for ( y = 0; y < 4; y++ )
 

  a_file<< "    if ((x == " << x << ") && (y ==" << y << "))\n    {"
    << "\n        string word2 = question(,,,); \n    }\n\n";
 
  }
a_file.close();

}



Last edited on
Read the entire file into an array of strings (each string is a line of the file). Then you can access the lines randomly.

Those are all the operations a file permits.
Last edited on
Great, that sound just like what I need. 2 quick questions, if its ok.

1) "Read the entire file"? If that file happens to be a fairly large file, might I run into memory issues?

2) "access the lines randomly." I need controlled line by line access. Will this allow me to do that?
1. Are you planning on using a file larger than ~500 MB?
2. It will allow you to tell your program "give me line 412".
No, not with with program. I am just curious. It seems like a logical possiblity tho. I might deside to use this same program later, and wanted to know if it would work on a much larger file. Thanks all. For now, I doubt I will be dealling with more than a couple Mb.

That sounds like exactly what I need. Thank you. But what is? Ill go google "array of strings", and hope I find it.

Thank you,again, Helios. Youve been a huge help.
Um...
Just use an std::vector<std::string>. We have a reference here for a reason.
I know helios. I have been using the reference files here, along with the tutorials found at www.cprogramming.com , and several beginners guides to C++ Ive dl'd. Plus I google several times a day.

All in all I have to say I am getting a great introduction from all of these sources. I just cant find what I need to finish this like program. Probly because it is outside my skill range atm. My beginners guides dont cover anything but the most basics file I/O, and when looking for more info, everything I find is to advanced for me to understand right now.

Helios, Thank you for your help. I will just have to come back to this problem later when I have a better grip on C++. But for now, unless some one is willing to actually post the code here.(Im sure that is not likely to happen.) I will just have to finish this piece of my code the good ol' ctrl+C/ctrl+P way.

Helios, thats again for all your attention to my problem. Im sorry we werent about to solve it yet. But I know Ill figure this out eventually.

std::getline() is the only I/O operation you'll need. I don't really know what kind of fancy file operations you expect to find.
I know, and I have looked at
http://www.cplusplus.com/reference/iostream/istream/getline.html
I just dont know how to use it to do what I want. Its just something I dont get right now.

Im not looking for anything fancy. Just a piece of code that says:

open file("QnA.txt")
get line (43)
append to file("list.cpp")

------------------------------------------

=] HAHA, I think I just now came up with an idiot's solution*! Thanks helios for helping me keep this problem on the table.

Its crude but it is better than doing it by hand.

If I can figure out how to make iostream use the value of an int for the file name I will have it.

I can save each value as a file. Name the files 1, 2, 3...

Then using a for loop... YAY!


*
An "Idiot's solution" is a term I like to use for an idea that MIGHT!!! work. Its called an idiot's solution because its usually something so simple it takes and idiot to think of.

Its part of my "If you cant fix it, break it till it works." philosophy
No. That's an idiot's solution because it's terrible.
Your 1,2,3 steps are possible. All you need to do is read up to line 43 instead of getting line 43; then copy line 43 to your new file.
Your philosophy must be entertaining, I would love to use it if I wasn't afraid of doing more damage than I started with.
I prefer "If you can't make it work, make something else that works just like it"
See? Even the newb sees a somewhat better solution.

The real solution here is to read ALL the lines in the file, each of them going into an std::string that's inside an std::vector.

I feel more inclined towards this: http://img182.imageshack.us/my.php?image=1198849716799mb3.jpg
ROFL!!! Thats made my day, I challenge anyone to have a better philosophy than that. Perfect, just Perfect.
Last edited on
bah!
Some one just wanna write a couple lines of code for me and show me how its done?

Just a tiny little code...

1
2
3
 open file "blank.txt"
getline 3
cout << line 3


Once I see the code Ill know what yall are talkin about. Then Ill be able to use it, and make my program do what it should do.

Might have been easier to just start with that. sorry for the trouble.

Umz,
That way of thinking is more for real world situations. Say you have a device that doesnt work any more, at all, and it cant be fixed. Now the only real solution is to scrap it and get a new one. But when you cant (or dont wanna) get a new one, for any reason at all, and you just want something that "works". It makes a lot of sense.

example:
One of the wheels falls of your roll-around table, and you lose it. Now your table dont work! Remove the other 3 wheels, and at least your get a functioning table.
O U!
I'll do it just because it's short and I like you:
1
2
3
4
5
6
7
8
9
10
11
std::ifstream file("file.txt");
std::vector<std::string> arr;
arr.push_back("");
while (1){
	std::string s;
	if (!std::getline(file,s))
		break;
	if (s.size()>0)
		arr.push_back(s);
}
file.close();

I really wanted to do something that didn't require making copies of copies, but I thought it would be best to follow the KISS philosophy.
Last edited on
Topic archived. No new replies allowed.