What am I doing wrong?

This is a discussion on What am I doing wrong? within the C Programming forums, part of the General Programming Boards category; When I compile this part of my program it will complie correctly, but when I run the program it always ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    What am I doing wrong?

    When I compile this part of my program it will complie correctly, but when I run the program it always crashes. I have narrowed it down to the prinf statement right after I scan in the files. What am I doing wrong here?
    (I din't include the rest of the program because my problem lie's within this area)

    Code:
    #include <stdio.h>
    
    
    char lname[20], fname[15], phoneNo[12];
    int menuchoice, record, cnt, totalcnt;
    FILE *infile, *outfile;
    
    
    	struct record{
    			char lname[20];
    			char fname[15];
    			char phoneNo[12];
    	};
    
    
    int menu(struct record phonebook, int);
    void addrecord(struct record phonebook, int);
    void deleterecord(struct record phonebook, int);
    void editrecord(struct record phonebook, int);
    void search(struct record phonebook, int);
    void view(struct record phonebook, int);
    void sort(struct record phonebook, int);
    
    
    
    
    int main (void)
    {
    
    struct record phonebook;
    char filename[20];
    
    printf("\n\nEnter file name to open: ");
    scanf("%s", &filename);
    infile=fopen(filename,"r");
    if (infile == NULL){printf("Unable to open file");return(0);}
    
    
    while(fscanf(infile, "%s %s %s", &phonebook.lname[cnt], &phonebook.fname[cnt], &phonebook.phoneNo[cnt]) != EOF) cnt++;
    printf("%s %s %s", phonebook.lname[0], phonebook.fname[0], phonebook.phoneNo[0]);
    
    totalcnt = cnt;;
    
    return 0;
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    //char filename[20];
    You should give more space.. char filename[50]; will be fine

    //scanf("%s", &filename);
    use fgets() to collect strings from streams

    You cannot give as input something like that:
    Code:
    c:\programming\file.out
    You need to double '\', look:
    Code:
    c:\\programming\\file.out

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I have a txt file that includes this in it:

    Young John 4073823481
    Brown Fred 2738230116

    I also have it stored in the same directory so when I open it I only have to do is type data.txt and it will open the file... I am still getting an error.. I have attached the ziped file of all the code and the data file and the compiled program so you can see what I mean.
    Attached Files Attached Files

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You have a lot of bugs in your code.
    About your file that won't work, look here:
    Code:
    char filename[50];
    
    printf("\n\nEnter file name to open: ");
    scanf("%s", &filename);
    if((infile = fopen(filename, "r")) == NULL) {
        perror("Could not open the file\n");
            exit(1);
    }
    This should work fine, try input like that:
    Code:
    c:\data.txt
    or
    c:\\data.txt
    Now, what you mean here?
    Code:
    phonebook.lname[0] = "Banks";
    phonebook.fname[0] = "John";
    phonebook.phoneNo[0] = "885-0983";

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Vber


    //scanf("%s", &filename);
    use fgets() to collect strings from streams

    You cannot give as input something like that:
    Code:
    c:\programming\file.out
    You need to double '\', look:
    Code:
    c:\\programming\\file.out
    you lost me here
    hello, internet!

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    someone once in the forum told me to do this...
    and for me it work's at least, something wrong with that?

    I tried again and both / and // works here.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    oops...

    --------------------------------------------------------------------------------
    phonebook.lname[0] = "Banks";
    phonebook.fname[0] = "John";
    phonebook.phoneNo[0] = "885-0983";

    was a test and one of the last times I saved it.. that isn't in the real program. I am not having a problem with the scanning as much as I am when I print it. Unless the scanning is the error. I removed the printf statement right after the scaning part and the program ran correctly till I tried to print out the values that I scaned in. I still recieved this error when I programed in the values of the structures. like you see above. So what is wrong with my structure's or my printing?

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Look..

    You got a lot of serious errors here, example:
    printf("\n%s %s %d", phonebook.lname[i], phonebook.fname[i], phonebook.phoneNo[i]);
    In your struct you defined phoneNo as char, so how do you espect %d will work here? and the input you receive for the user as number will work?

    Hmm, now I'am going out, but, look:
    For copying strings, use:
    strcpy()

    And inputing string from a stream, use fgets()
    Hmm, fix your struct, this is the main problem of your program.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Look..

    >>In your struct you defined phoneNo as char
    No, they defined it as an array of char, which is a different thing altogether.

    Code:
    while(fscanf(infile, "%s %s %s", &phonebook.lname[cnt], &phonebook.fname[cnt], &phonebook.phoneNo[cnt]) != EOF) cnt++;
    This won't work, you have only got one struct, not an array of structs. You need something like this:
    Code:
    struct record MyRecords[MAX_RECORDS];
    
    while(fscanf(infile, "%s %s %s", &phonebook[cnt].lname, &phonebook[cnt].fname, &phonebook[cnt].phoneNo) != EOF) cnt++;
    
    /* sometime later */
    printf("%s %s %s", phonebook[cnt].lname, phonebook[cnt].fname, phonebook[cnt].phoneNo);
    Note that I didn't put an array subscript on the struct elements, but on the struct itself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    No, they defined it as an array of char, which is a different thing altogether.
    Right, I forgot, but still, he cant print this like %d.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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