What am I doing wrong ? A little help please

This is a discussion on What am I doing wrong ? A little help please within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> FILE *fp; int main() { char N; char first[8]; char last [9]; char middle[11]; printf("INPUT :"); fp = ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    What am I doing wrong ? A little help please

    #include <stdio.h>

    FILE *fp;

    int main()
    {

    char N;
    char first[8];
    char last [9];
    char middle[11];

    printf("INPUT :");
    fp = fopen("NAMES.DAT", "N");
    while ((N = getc(fp)) != EOF)
    {
    scanf("%s , %s\\", &first, &last);
    printf("%s, %s\\", first,last);
    }

    fclose(fp);

    printf("OUTPUT:");
    printf("%s, %s", last, middle);

    return 0;

    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > char N;
    This should be
    &nbsp; int N;
    otherwise your comparison with EOF will always fail (or might do)

    > fp = fopen("NAMES.DAT", "N");
    "N" is not a valid open mode, try "r"

    > scanf("%s , %s\\", &first, &last);
    first and last are already arrays, so
    &nbsp; scanf("%s , %s\\", first, last);
    is all you need

    > while ((N = getc(fp)) != EOF)
    So you burn a character, then read two strings.
    This doesn't sound right to me....

    > printf("%s, %s", last, middle);
    You never read middle, so all it will print is garbage

    How am I doing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    You are super, super moderator. Thank You very much.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    Can I still use this

    while ((N = getc(fp)) != EOF)

    to read in the middle character.

    I am reading names off a file called names.dat and putting them in order.

    James Bagwell result would be Bagwell, J.

    This is what I am trying to do with 10 names. Can I use this for the Bagwell, J. Just the J character.

    Thank You

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Code:
    while ( fscanf( fp, "%s %s %s", first, middle, last) != EOF ) {
        printf( "%s %s %c\n", last, first, middle[0] );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03: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, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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