Thread: accessing sequential files "newbie"

  1. #1
    Registered User
    Join Date
    May 2009

    accessing sequential files "newbie"

    i have a sequential file named number.txt it contains the numbers 10 thru 20 . i need to access this file and add a value of one to each number. then create and write to a file called updatedNumbers.txt. this is for school. im trying to get a c to pass. i have took this class twice already. it is online with limted help. i'm so confused and i don't understand a lot of what's going on. i think i make take up heavy drinking if i don't figure out some thing soon. i would appreciate any help. i have posted a thread earlier on another project but have abandoned it it's too much for me. but any way here is my program any advice would be helpful and greatly appreciated.

    #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()
    //declare variables
    	string name = "";
    	int num    = 0;
    	int updatedNum
    	//open and read the file 
    	ifstream inFile;"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;
    		} //end while
    		//close the file
    		cout << "The file could not be opened." << endl;
    	//end if
    //create file object and open the file
    	ofstream outFile;"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
        return 0;
    }   //end of main function

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    At a high level this is one way of doing this:
    1. Open input/output files.
    2. Read number from input file.
    3. Increment number and write to output file.
    4. Go back to step 2.

    As to your current code, it does not compile due to syntax errors, undeclared variables, num not being an array, etc... It is however reasonably close to a working example.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Have you tried using C strings? I find them much more easy to use and give me total control always. Just try it and see how it comes up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing files.
    By omnificient in forum C Programming
    Replies: 29
    Last Post: 03-09-2008, 02:33 PM
  2. Accessing files from a C program.
    By Sea (C) Maniac in forum C Programming
    Replies: 13
    Last Post: 12-21-2007, 05:50 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. C++...sequential access data files
    By locolobo in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2002, 10:06 AM