Thread: files

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

    files

    sry for posting another thread on the same subject but i'm having problems. i'm opening files using "r+b" and the data is that im saving is filling from its position onwards what should i do to save only in one position

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should generally post code if you have a question.

    But look at this page > fopen(3): stream open functions - Linux man page.

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



    i'm saving a structure in a .dat file. the file is being opened by "r+b". My problem is that when im saving the data, it saves on its position and all the other position behind it what should i do?? showing where the saving of imp
    Code:
    void readbook(booking_type *bookings){
    int check =0;
    int size =0;
    
    int i =0;
    bookings->imp = 1;
    printf("\n");
    }
    showing where is the program saving the data
    Code:
    file = fopen(filename, "r+b"); 
    fseek(file,sizeof(booking_type)*position,SEEK_SET);//position where to save the information
    readbook(&bookings);
    fwrite(&bookings,sizeof(booking_type),1,file);
    if (file == NULL)
    {
    printf("Error opening file %s.\n", filename);
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I cant understand the question, but I can see a bug in the code.

    Check file for NULL straight after the call to fopen, before you try and fseek within it. Also, if it is NULL, actually do something to alter the program's flow. e.g. return.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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