Why won't this work?

This is a discussion on Why won't this work? within the C Programming forums, part of the General Programming Boards category; I am reading a file in a loop, the following line reads from the file Code: char var1[2000]; int rn; ...

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    Why won't this work?

    I am reading a file in a loop, the following line reads from the file

    Code:
    		
    char var1[2000];
    int rn;
    //etc......
    
    
    		rn=fscanf(textfile,"%[^\n]\n",var1);
    Here is a bit of the file it reads which has a problem.
    It reads each line into var1 as expected however it does not read
    the line which starts with a space eg " Game #7533141350 starts. "
    It completely loses it and carries on OK as if he line never existed.
    Why is this? I think I have came across this before but forget how I
    fixed it. As it happens it's not a problem as that line is not important
    for what I am doing but I would like to know what I am doing wrong
    and how to fix it!!

    Code:
    GT_champ shows [ 5c, Ac ]two pairs, Fives and Twos.
    GT_champ wins $0.14 USD from the main pot with two pairs, Fives and Twos.
     Game #7533141350 starts.
    
    #Game No : 7533141350 
    ***** Hand History for Game 7533141350 *****

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >rn=fscanf(textfile,"%[^\n]\n",var1);
    Keep in mind that any whitespace in the format string means "any kind and amount of whitespace". If you just want to extract the newline after the scanset, it's best to do so after fscanf so you have more control:
    Code:
    rn=fscanf(textfile,"%[^\n]",var1);
    fgetc(textfile); /* Trim newline */
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Are you trying to simply read each line into var1?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Are you trying to simply read each line into var1?
    The evidence suggests yes:
    Quote Originally Posted by esbo
    It reads each line into var1 as expected [...]
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Thanks for the clarification Prelude, guess that's what happens when I skim over the post. Well the posted code doesn't run into issues on my box.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Actualy just noticed that when I look at it in the editor is shows a null
    at the start of the missing line.

    Last time I looked it appeared to be a space. Not sure why it change
    how it was displayed in notepad++

    Code:
    6f 73 23 od oa 00 47 61
     o  s    .               G  a
    So... I presume it does work but when I try to print what I have read
    or do anything with it it reads the null as the end of string and ignores
    the rest.

    Is what I am doing the best way to read from the file?
    Last edited by esbo; 11-14-2008 at 02:51 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Using
    while ( fgets( buff, sizeof buff, fp ) != NULL )
    in the first instance would be best.
    Then you can do whatever you want with buff when it's in memory.

    Mixing input with validation and conversion all in one call is just too hard to get right without a hell of a lot of messing about.
    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. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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