sequentail access file

i have a sequential access file named number.txt which contains the numbers 10 thru 20 I'm trying to write a program that reads the number.text file and add a value of one to each number and then write it's to a sequential file called updatedNunber.txt. I'm so confused I'm about to take up heavy drinking.if anyone one could help me clean up my program i appreciate a lot a whole lot.


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

using std::cout;
using std::cin;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::ios;
using std::string;

int main()
{

string name = "";
int num = 0;
int updatedNum = 0;


ifstream inFile;
inFile.open("numbers.txt", ios::in);

//determine whether the file was opened
if (inFile.is_open())
{
//read a record
getline(inFile, name);
inFile >> num;
while (inFile.eof())
{
//display the record
cout << name << " " << endl;
//read another record
getline(inFile, name);
inFile >> num;
}


inFile.close();
}
else
cout << "The file could not be opened." << endl;


//create file object and open the file
ofstream outFile;
outFile.open("updatedNumbers.txt", ios::out);

//write the updated numbers to the file
outFile << heading << endl << endl;
outFile << columnHeaders << endl;
outFile << underLines << endl;
for (int x = 0;x += 1)
outFile << num[x] << endl;
//end for
outFile << endl << ;

//close the file
outFile.close();


return 0;
}

Last edited on
DO use code tags to preserve indentation when posting code.

DON'T use stupid "endif", "end for", "end of main function" type of comments.

DON'T use a comment that merely states in english what the command does:
1
2
//close the file
inFile.close();

Other than that, your code has variable names that aren't declared and generally makes very little sense.

Try again.
thanks
@Hammuabi,
you didnt help him clean any of his code what so ever, you just told him to remove a few comments?

dont use
1
2
 while(infile.eof())
 

use..
1
2
string line = "";
while(getline(infile,line)

you could use a different flag to make it not delete the contents of the file everytime.
I think ios::app works .

you dont have to write this out like this
1
2
3
outFile << heading << endl << endl;
outFile << columnHeaders << endl;
outFile << underLines << endl;

this just looks cleaner but i dont think its more efficient
1
2
3
4
outFile <<heading<<endl<<endl
           <<columnHeaders<<endl
           <<underlines<<endl;

In your for loop
 
for (int x = 0;x += 1)

write it like this
 
for(int x = 0; x++)





you could add a cin.get();
to the end to stop the program from
closing on its on or even worse just add a system("pause");
either way.

oh and instead of using all of those std declarations you could just
use using namespace std; to knockem all out but it will increase
your file size.
Last edited on
closed account (S6k9GNh0)

In your for loop
for (int x = 0;x += 1)
write it like this
for(int x = 0; x++)


If you don't know the reason, x++ is NOT the same as x += 1. At least on the inside they aren't...In a benchmark it's also shown that using the increment value is faster than the ladder. Though this link doesn't prove it, it shows other things.
http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/increment.html
Last edited on
Topic archived. No new replies allowed.