Thread: Problems with saving files

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    27

    Problems with saving files

    hi there. New to the forums here so I apologise if this thread is in the wrong place

    I've written a program that opens a text file and reads the data off it, and allows you to edit the information or add more stuff thats to it. When I go to save it, it asks for the filename to save it under, gives a little update on what you're saving, and then saves it.

    So far it does all of that apart from save the file. When I go to save it, the size of the text file just keeps on increasing till my hard drive is full. It recon it must be looping the data that is sent to the text file somewhere but I don't know where.

    Here's the part of my code that I use to save it, if I've not given enough of the code, just say I'll post more.

    Code:
    // SAVE STOCK DETAILS TO A TEXT FILE
    void save_items( int numitems)
    {
            //char filename [12];
            cout << "Type the name of the data file...  ";
            cin >> filename;
            cout << "this is the file name.." << filename << "\n";
            system ("pause");
    
            int  i = 1;
    	FILE *out_file;
    	cout << "\n\t\tDefault filename is " << filename <<  " with " << numitems-1
    << " items\n";
    	cout << "\n\t\tSaving " << filename << ".......";
    	waitforkey( );
    	out_file=fopen(filename,"w");
    	do
    	{
    		fprintf(out_file,"%7d %25s %25s %7d %7.2f \n", slist[i].stocknumber,
    		slist[i].description,   slist[i].supplier, slist[i].quantity,
    		slist[i].price);
    		i = i++;
    	}
    	while (i <= numitems-1);
    
    	cout <<"\nSaved a total of" << i-1 << " items\n";
    	fclose (out_file);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    i = i++;
    doesn't chang the value of i.
    use
    Code:
    i++;
    EDIT: actually it is undefined behavour. look up sequence points.

    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i = i++;
    Maybe this
    http://c-faq.com/expr/ieqiplusplus.html

    The fix is just to say
    i++;

    > int i = 1;
    Also, arrays start at 0, not 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    27
    dang, it was that simple and I completely missed it.

    thanks a bunch

    I've got int = 1 because the program lists all the items in the text file and numbers them. I've got the numbers starting at one not zero so I the first space in my array is blank, so thats why I'm starting it at 1
    Last edited by akira181; 05-13-2006 at 01:46 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    27
    got another question, wonder can anyone help with it

    Currently, the save function asks for the filename, tells you what you entered then saves it.

    Instead of it just telling you what filename you entered, how would I make it confirm the filename and give you a choice whether to continue to save it or go back and change the filename?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could put it in a loop, and if the users does a "confirm filename" action, break from the loop and save the file.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems saving audio files
    By JoeJTaylor in forum C++ Programming
    Replies: 0
    Last Post: 04-23-2006, 03:10 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  3. Replies: 6
    Last Post: 08-13-2003, 04:35 AM
  4. Problems openning huge files
    By acoelho74 in forum C Programming
    Replies: 4
    Last Post: 02-11-2003, 07:02 PM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM