Problems - writing File

This is a discussion on Problems - writing File within the C Programming forums, part of the General Programming Boards category; Code: void add_seating(struct_seating seating_temp) { FILE *fp_index, *fp_seating; int i = 0; int j =0; int cont =1; int continue_index ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Problems - writing File

    Code:
    void add_seating(struct_seating seating_temp)
    {       
            FILE *fp_index, *fp_seating;
            int i = 0;
            int j =0;
            int cont =1;
            int  continue_index = 1;
    
            indexS index_temp[FILE_SIZE];
            //struct_seating seating_temp;
            
            fp_index = fopen("array_seating_index.bin", "r+b");
            
            if(fp_index == NULL)
            {
                    fprintf(stderr, "Error, File: array_seating_index.bin");
            }
            
            fp_seating = fopen("array_seating.bin", "r+b");
            
            if(fp_seating == NULL)
            {
                    fprintf(stderr, "Error, File: array_seating.bin");
            }
            fread(&index_temp[0],sizeof(indexS),1,fp_index);
          
            while(cont ==1)
            {
                    j=0;
            
                    fseek(fp_seating, i*sizeof(struct_seating),SEEK_SET);
            printf("Value is: %d\n", seating_temp.sectionA[0]); //prints correct value of sectionA[0]
                    fread(&seating_temp, sizeof(struct_seating),1,fp_seating);
            printf("Value is, before if: %d\n", seating_temp.sectionA[0]); //Doesnt print correct value of sectionA[0]
           
                    if(seating_temp.event_num == 0)
                    {
                            printf("Value is: %d\n", seating_temp.sectionA[0]);
                            fseek(fp_seating, i*sizeof(struct_seating),SEEK_SET);
                            fwrite(&seating_temp, sizeof(struct_seating),1,fp_seating);
                                            
                            while(continue_index ==1)
                            {
            
                                    if(index_temp[j].index_event_num == 0)
                                    {
                                            //printf("equal to 0\n");
                                            index_temp[j].index_event_num = seating_temp.event_num;
           index_temp[j].index_seating = i;
                    
                                            fseek(fp_index, 0*sizeof(indexS),SEEK_SET);
                                            fwrite(&index_temp[0],sizeof(indexS),1,fp_index);
                                            continue_index = 0;
                                    }
                                    else
                    
                                    if(j == FILE_SIZE)
                                    {
                                            printf("File ***** full!\n");
                                            printf("j is: %d\n");
                                            continue_index = 0;
                                    }
                                    j++;
                            }
                                            
                            cont = 0;
                    }
                    else
                    if(i == FILE_SIZE - 1)
                    {
                            fprintf(stderr, "File full!!!\n"); 
                            cont = 0;
                    }
                    else
                            i++;
            }
                                            
            fclose(fp_seating);
            fclose(fp_index);
                                     
    }
    There are two comments there that show the value of sectionA[0] are changed. If I take out he fread, where the comments are, the value remains okay, however I never have anything written to the file, and I dont know why.

    Anyone?

    Thanks.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    After fixing several functions, I have a few more questions, and a little puzzled.

    I pass the fucntion a struct, when I perform the first fread, is it not reading from the stream, which is a file?

    If so, my struct is never read, but what is in the file is.

    Can someone explain this for me?

    Thanks again.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    After looking at the code, I realized the problem.

    I was writing a dummy record to the file, instead of the one passed as a param.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21