File I/O logic error

This is a discussion on File I/O logic error within the C Programming forums, part of the General Programming Boards category; Hi, I'm quite a newbie around C, and I am trying to make a program that, among other things, asks ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Madrid, Spain, Spain
    Posts
    8

    File I/O logic error

    Hi,
    I'm quite a newbie around C, and I am trying to make a program that, among other things, asks the user for his name, surname and telephone number and saves it to a file.
    I've managed to save the name and surname correctly, but when it comes tu the telephone, the program saves bizarre numbers instead of the one typed by the user.

    The function in charge of file creation is as follows:
    Code:
    int save_profile(char type) { 
        char surname[N];
        profile user;  // A structure defined through typedef
        FILE *puser;
        
        fflush(stdin);
        printf("Insert your surname: ");
        gets(user.surname);
        
        strcat(user.surname, ".txt");
        if ((puser=fopen(user.surname,"w"))==NULL) {
            printf("Error creating your profile\n");
            system("PAUSE");
            return 1;
        } else {
            printf("Insert your name: ");
            gets(user.name);
            fputs(user.name,puser);
            fprintf(puser,"\n");
            fflush(stdin);
            
            fputs(user.surname,puser);
            fprintf(puser,"\n");
            fflush(stdin);
            
            printf("Insert your phone number: ");
            scanf("%c",&user.phone);
            fprintf(puser,"%d",user.phone);
            fflush(stdin);
            
            fputc(type,puser);
            return 0;
        }
        
        fclose(puser);
        
    }
    If the user types, for example, "John" as his name, "Walter" as his surname and "6123123123" as his phone number, the filename will be Walter.txt and it will contain:

    John
    Walter
    651241A <---- This is the cause of my headache

    (I've tried trating the phone number both as an array of ints and as a string of chars, no difference)
    Should I fopen the file in "wb" mode?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    And, you defined "user"/profile as what?
    Post the structure declaration.

    The user.phone is of what type?

    This line implies it is a single character.
    Code:
    scanf("%c",&user.phone);
    This line implies it is a integer.
    Code:
    fprintf(puser,"%d",user.phone);
    It should be an array of characters. Using "%s" as printf/scanf.

    Flushing stdin in is undefined; therefore you should NOT do it.
    Code:
    fflush(stdin);

    Tim S.
    Last edited by stahta01; 05-09-2012 at 12:50 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Madrid, Spain, Spain
    Posts
    8
    The different treatment of user.phone is just a result of me trying to blindly fix the error. Originally, it was defined and treated as a string throughout the program.
    Thanks for your help, I found and fixed the problem which was, apparently, related to the definition of a related variable in another function.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Never use either gets() or fflush(stdin)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logic error? replace characters in a file
    By bos1234 in forum C Programming
    Replies: 3
    Last Post: 04-20-2012, 10:18 AM
  2. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 06:42 PM
  3. I think it's logic error
    By c++ in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2009, 08:02 PM
  4. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 09:58 PM
  5. logic error I think
    By sscook69 in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2002, 10:08 PM

Tags for this Thread


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