fscanf() problem

This is a discussion on fscanf() problem within the C Programming forums, part of the General Programming Boards category; Hey guys. I've been having a problem with reading a single char from a file. Everytime I get to that ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    37

    fscanf() problem

    Hey guys. I've been having a problem with reading a single char from a file. Everytime I get to that point, the program crashes. I'd appreciate any advice you may have.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Code:
    FILE *fp = fopen("file.txt", "r");
    c = fgetc(fp);
    printf("%c", c);
    does this work?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks Brian, but this is not exactly what I was looking for. I am reading through several items in the file. I have successfully read an int and two strings(jumping over the '/' delimiter of course). However, when I attempt to read a char which comes afterwards, the program crashes.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    A common thread with scanf() victims is forgetting to pass the address of the variable in which to store the value.

    Right:
    Code:
    {
      char ch;
    
      scanf("%c", &ch);
    }
    Wrong:
    Code:
    {
      char ch;
    
      scanf("%c", ch);
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks itsme86. You're quite right. I'm laughing at myself right now. I thought I had already gotten over this one. Thanks again.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Consider this broken code, the compiler command line and the compiler output.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( ) {
        int foo;
        scanf( "%f", &foo );
        return 0;
    }
    
    gcc -Wall hello.c
    hello.c: In function `main':
    hello.c:5: warning: float format, different type arg (arg 2)
    Yes, gcc will diagnose incorrect parameters passed to printf and scanf type functions, so you need never worry about missing an & again, and wondering what is going on when it doesn't work properly.
    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. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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