Thread: Need some help with files

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    7

    Exclamation Need some help with files

    my program should accept some structures variables form the user in a structure array, write those variables to a txt file then read the data from the txt file and print them but the program keeps crashing after writing to the text file. I think the problem is with my booksearch() function but i cant seem to diagnose the problem. The function was working some time back but was just reading a lot of garbage from the file (and i verified the file had all the correct data that was inputted) so i made some changes and now cant get it to work.

    im using sequential access file so please point me to my errors and any suggestions would be appreciated.

    my code so far:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    void booksearch();
    void enterstuddata();
    void writetofile();
    //-------------------------------------
    struct studentfile{
        char s_name[30];
        int s_id;
        char s_faculty[40];
        int s_contactnum;
        float s_loanbal;
        int s_brrwamnt;
    };
    
    
    
    
    void main()
    {
        struct studentfile s[10];
        enterstuddata(s);
    
    
        booksearch();
    }
    
    
    //-------------------------------------
    void enterstuddata(struct studentfile stud[])
    {
        int count, size, *s_ptr;
        size = 2;
        for(count=0;count<size;count++)
        {
            printf("Enter student name: ");
            fflush(stdin);
            gets(stud[count].s_name);
    
    
            printf("Enter student ID: ");
            scanf("%d", &stud[count].s_id);
    
    
            fflush(stdin);
            printf("Enter student faculty: ");
            gets(stud[count].s_faculty);
    
    
            printf("Enter student contact number: ");
            scanf("%d", &stud[count].s_contactnum);
    
    
            printf("Enter student loan balance: ");
            scanf("%f", &stud[count].s_loanbal);
    
    
            stud[count].s_brrwamnt = 0;
            printf("\n");
        }
        s_ptr = &count;
        writetofile(stud, *s_ptr);
    }
    //-------------------------------------
    void writetofile(struct studentfile stud[],int size)
    {
        int count;
        FILE *studfile;
    
    
        studfile = fopen("students.txt", "w");
    
    
        if(studfile != NULL){
            for(count=0;count<size;count++){
                fprintf(studfile, "%s %d %s %d %f %d\n", stud[count].s_name, stud[count].s_id, stud[count].s_faculty,
                stud[count].s_contactnum, stud[count].s_loanbal, stud[count].s_brrwamnt);
            }
            fclose(studfile);
        }else{
            printf("ERROR!!: StudentData file could not be opened for writing");
        }
    }
    //----------------------------------------
    void booksearch(){
        FILE *bkptr;
        char name[30], fac[40];
        int id, num;
        float bal;
    
    
        bkptr = fopen("students.txt", "r");
    
    
        if(bkptr != NULL){
            fscanf(bkptr, "%s %d %s %d %f", name, &id, fac, &num, bal);
            fclose(bkptr);
            printf("%s\n %d\n %s\n %d\n %f\n", name, id, fac, num, bal);
        }else{
            printf("ERROR!!: BookData file could not be opened for writing");
          }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I see three bad things right off the bat:

    1. main returns int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    2. fflush(stdin) results in undefined behavior: FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    3. Do not use "gets", it is a very unsafe function: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Look at line 98, something is missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2012, 03:25 PM
  2. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  5. 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

Tags for this Thread