Thread: reading a char from file

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    reading a char from file

    what I am needing to do is read a character from a file into a structure member. I thought it would be simple enough but fscanf, I guess just works for strings. I commented out tons of stuff and put a printf statement after every fscanf and the character line is where it falls apart. The preceding members are strings and after hours of testing everthing else I finally came to the conclusion the fscanf does not return anything that fits neatly into a char.
    Code:
    typedef struct
    {
       char mem1[10];
       char mem2[10];
       char mem3[10];
       char mem4;
       int mem5;
       }struct_a
    FILE *fout;
    
     
     fscanf(fout, "%s", struct_a.mem1);
     fscanf(fout, "%s", struct_a.mem2);
     fscanf(fout, "%s", struct_a.mem3);
     fscanf(fout, "%c", struct_a.mem4); /*this is the line that's a problem*/ 
    fscanf(fout, "%d", struct_a.mem5);/*haven't tested to this line yet either*/ 
    I was hoping to be able to continue using fscanf so as not to lose my place in the file.

    Hopefully the preceding is enough to give you an idea of what I am doing without weighing the post down with a bunch of extra stuff.
    Last edited by suzee_q00; 03-11-2008 at 06:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Can the character you want to read in be a space? If not, use
    Code:
    fscanf(fout, " %c", struct_a.mem4);
    which will skip past the newline (from the line before) that you're reading in instead.

    Edit: and someone has asked this question recently, so you should be able to find that discussion if you search.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Quote Originally Posted by tabstop View Post
    Can the character you want to read in be a space? If not, use
    Code:
    fscanf(fout, " %c", struct_a.mem4);
    which will skip past the newline (from the line before) that you're reading in instead.

    Edit: and someone has asked this question recently, so you should be able to find that discussion if you search.
    Thanks for the reply. It actually worked! I can't believe I wasted hours of my time because of a stupid space. If you could supply more info on the previous post I would be happy to go look that up. I did try a search, being certain that I was not the first person to ever want to retrieve a single character from a file. Perhaps I just don't know what search terms to use.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I used "input character" as the search term -- it was a little hard to find because the thread was actually called "switch statement problem" or something like that.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also %c and %d formats need pointer to variable - overwise it will crash
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM