Advice sought?

Hi, I'm trying to create a program that when run ten times (closing inbetween) then throws up a message. My thoughts on this is to get my application to create a dat file storing 0 (Zero) in it, then have my application read the dat file for input each time it runs adding (++) to the dat file till ten runs. Thinking <fstream> get, to extract from dat file. ofstream outfile, ios::trunc, to write to file. All in a while loop. Looking at using string to int to covert on get (although I don't know how yet! or even if needed?) Now before I get to carried away I need someone to say "what the hell am I doing it this way for?" He should just (insert your expertise and advice here). Yes I'm a noob, hence this question, "Do I need to close my dat file inbetween my read/ write to it, ie get information, close file, open file again to write new data to it? No real code as yet, got my programs that get information from a file and one that writes to a file, while loops not a problem, string to int I'll work out or get back to you all. Now before I try putting it all together I thought I'd seek advice on is this the correct way to go about it?
Thanks in advance, Leppie

Recommended Answers

All 3 Replies

It seems like you are explaining two things. The first is that the program opens, reads a file, updates the file, then closes. The second is that you are doing this read/write in a while loop?

fstream is definitely the way to go for the file IO. Then you should use a stringstream to convert the string you read to the type (int) you want.

These may help you with some syntax:
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines
http://programmingexamples.net/index.php?title=CPP/IO/FileOutput
http://programmingexamples.net/index.php?title=CPP/IO/FileInput

David

Thanks David, I'll examine the links and try to proceed, one quick question, does the file need to be closed between inputting and outputting or can I do both operations to the file and just close it at the end? (If it's answered in the links then sorry for the stupid question).
Once again thanks.

Thanks David, I'll examine the links and try to proceed, one quick question, does the file need to be closed between inputting and outputting or can I do both operations to the file and just close it at the end? (If it's answered in the links then sorry for the stupid question).
Once again thanks.

It depends on how you configure the stream. An ifstream can only input (read), an ofstream can only output (write), but a fstream can do both.

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.