Thread: File I/O question... please help!

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    File I/O question... please help!

    Please help me with the following code!! I want to have a file that contains a list of recipes, with the first line of the file being the number of recipes. To test out the file IO code, I wrote up this little test program, but it keeps overwriting the file!

    How can I make it append the data to the end, but change the first line at the end?

    Thanks so much!
    sirSolarius

    Code:
    #include <iostream>
    #include <fstream.h>
    
    void main()
    {
    	int *current, currenter;
    	currenter = 80;
    	current = &currenter;
    
    	ofstream CoolFile("C:\\recipes.dat", ios::app);
    	if (CoolFile)
    	{
    		CoolFile << "I'm" << endl;
    		CoolFile << "One" << endl;
    		CoolFile << "Crazy" << endl;
    		CoolFile << "Guy" << endl;
    		CoolFile.close();
    
    		ofstream numberFile("C:\\recipes.dat");
    		numberFile.seekp(0);
    		numberFile << *current;
    	}
    
    	ifstream CoolFileIn("C:\\recipes.dat");
    	char stringer[100];
    	int x;
    	for (x=0; x<6;x++)
    	{
    		CoolFileIn >> stringer;
    		cout << "\n" << stringer;
    	}
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    Re: File I/O question... please help!

    Originally posted by sirSolarius
    How can I make it append the data to the end, but change the first line at the end?
    I don't really understand your question. You want to append data to the end, that is not touching the rest of the data, but you want the first line at the end? Huh?

    Well I think your problem is:
    Code:
    ofstream CoolFile("C:\\recipes.dat", ios::app);
    Try this:
    Code:
    ofstream CoolFile("C:\\recipes.dat", ios::ate);
    Notice the ios::ate, it doesn't call ios::out so it won't overwrite the file. Or maybe that just won't work, I don't know. Maybe you could throw a ios::nocreate in there for good measure.

    Good Tutorial on File I/O.

    :)
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    Ok, here's what I'm saying:

    Everything should be appended to the end of the file, so when I make a new entry, it goes onto the end of the file.

    Example: (the desired file follows)
    Code:
    3
    Entry 1:
    Data
    Data
    Entry 2:
    Data
    Data
    Entry 3:
    Data
    Data
    Now I make a new entry:
    Code:
    4
    Entry 1:
    Data
    Data
    Entry 2:
    Data
    Data
    Entry 3:
    Data
    Data
    Entry 4:
    Data
    Data
    So the new stuff is appended, but the first line is changed. See what I mean?

    sirSolarius
    Last edited by sirSolarius; 10-05-2003 at 02:05 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The problem is that unless you're careful, it's dangerous to write straight to the top of the file, it could creep into the successive data and cause corruption. You could pad the top of the file with an amount of bytes that is non-variable in size, write to the top, seek the end of file, then continue to write.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    Originally posted by Sebastiani
    The problem is that unless you're careful, it's dangerous to write straight to the top of the file, it could creep into the successive data and cause corruption. You could pad the top of the file with an amount of bytes that is non-variable in size, write to the top, seek the end of file, then continue to write.
    Ok... but how do I even do that??

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    Can't anyone help me?

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    CoolFile.seekp(ios::beg);
    will position the ostream pointer to the beginning of the stream
    Code:
    CoolFile.seekp(ios::end);
    will position the ostream pointer to the end.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    The code still doesn't work.

    Code:
    #include <fstream.h>
    
    
    void main()
    {
    	int *current, currenter;
    	currenter = 80;
    	current = &currenter;
    
    	ofstream CoolFile("C:\\recipes.dat", ios::app);
    	if (CoolFile)
    	{
    		CoolFile << "I'm" << endl;
    		CoolFile << "One" << endl;
    		CoolFile << "Crazy" << endl;
    		CoolFile << "Guy" << endl;
    
    		CoolFile.seekp(ios::beg);
    		CoolFile << *current;
    		CoolFile.close();
    	}
    
    	ifstream CoolFileIn("C:\\recipes.dat");
    	char stringer[100];
    	int x;
    	for (x=0; x<6;x++)
    	{
    		CoolFileIn >> stringer;
    		cout << "\n" << stringer;
    	}
    }

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try this, might work.

    Code:
    #include <fstream.h>
    
    
    void main()
    {
                    char * currenter = "80";
                    char * filename = "recipes.txt";
    	ofstream CoolFile(filename);
                    CoolFile.close();
                    CoolFile.open(filename, ios::app);
    	if (CoolFile)
    	{
    		CoolFile << "I'm" << endl;
    		CoolFile << "One" << endl;
    		CoolFile << "Crazy" << endl;
    		CoolFile << "Guy" << endl;
    
    		CoolFile.seekp(ios::beg);
    		CoolFile << currenter << endl;
    		CoolFile.close();
    	}
    
    	ifstream CoolFileIn("C:\\recipes.dat");
    	char stringer[100];
    	int x;
    	for (x=0; x<5;x++)
    	{
    		CoolFileIn >> stringer;
    		cout << "\n" << stringer;
    	}
    }
    output I would like to see:

    80m
    One
    Crazy
    Guy

    At least that's what I would expect with if CoolFile were in binary mode and read() and write() were used to deal with file. Not sure what happens here however. ios::app may prevent the repositioning of the output file pointer using ios:: beg and behavior of << if file pointer is repositioned is unclear.
    --------------------------------------------------------------------------------

  10. #10
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I know ios::ate will let you to reposition put pointer (seekp()).
    If you use write() you could specify how many bytes you want to overwrite, but note that you won't be able to view the output using notepad
    Code:
    int num1 =1;
    int num2 =2;
    
    outFile.write(reinterpret_cast<const char *>(&num1), sizeof(int));
    outFile << bla... bla...
    outFile << ....
    outFile.seekp(ios::beg);
    outFile.write(reinterpret_cast<const char *>(&num2), sizeof(int));
    Using codes above, you'll always be able to overwrite the whole thing because you always write the same size (the size of an integer). If you view the output using VC++, you'll see it clearly.
    Last edited by alphaoide; 10-06-2003 at 08:51 PM.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    aplhaoide, how can u view the files if you use write()? Is the only way to read them back in or something? Or can u get a viewer of some sort?

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    If you view that output using VC++, you'll be able to read it. I don't know about Bloodshed. Otherwise, you need some hex dump utility.
    To read back the data, (equivalent to "infile >>"), just use read().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM