Thread: Files in c

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Files in c

    i'm try to save a structure containing a integer. when from 0 the integer is changing to 1 all the other positions is also changing what should i do ??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could post your code so we can see what you're doing.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Code:
    void readbook(booking_type *bookings){
    	int check =0;
    	int size =0;
    	
    	int i =0;
    	bookings->imp = 1;
    			printf("\n");
    imp is set to 1 when function is called
    Code:
    fseek(file,sizeof(booking_type)*add,SEEK_SET);
    	
    	fread(&bookings.imp, sizeof(booking_type), 1, file) ;
    	if(bookings.imp == 1) {
    	board[z][y][x] =1;
    	}
    	else{
    		board[z][y][x] =0;
    	}
    	switch(board[z][y][x]){
    						case books:
    							printf("X ");
    							break;
    						case notbook:
    							printf("_ ");
    							break;
    						}
    	      fflush(file);
    					}
    			printf("\n");	
    		}

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think flushing inputs is undefined.

    NOTE: You have too many global variables for me to considered it worth spending my time to help you.

    EDIT: You do realize that read/writing (fread/fwrite) to the same file is not always going to work; unless you open the file in binary mode and write only to the end of the file.

    Tim S.
    Last edited by stahta01; 04-22-2012 at 08:57 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by stahta01 View Post
    I think flushing inputs is undefined.
    You are correct. It is not defined.

    Quote Originally Posted by stahta01 View Post
    NOTE: You have too many global variables for me to considered it worth spending my time to help you.
    There are better reasons to not help someone new...

    Quote Originally Posted by stahta01 View Post
    EDIT: You do realize that read/writing (fread/fwrite) to the same file is not always going to work; unless you open the file in binary mode and write only to the end of the file.
    You can append to text files, too.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by WaltP View Post
    There are better reasons to not help someone new...
    I do NOT like playing twenty questions; I figure if they DO NOT give enough info to help then no reason for me to waste my time.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    i need to seek to a position write to that position only and go back to the begining. The main problem of this program is that when im writing to a position its keep on writing to the other position. i need something which stops when the info is written on that posiiton

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Replies: 3
    Last Post: 05-03-2002, 05:18 PM