problem using strtok and fscanf together

This is a discussion on problem using strtok and fscanf together within the C Programming forums, part of the General Programming Boards category; ok, im doing this program and i want the program to read from a file characters that are separated by ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    problem using strtok and fscanf together

    ok, im doing this program and i want the program to read from a file characters that are separated by periods, but i do not want the periods to be read in, so basically what i do is i read the string from a file, and then i use strtok and copy the tokens to another file. However, it only works for the first one, after that, fscanf wont read the next string, it returns -1. this happens only after it does the tokenization. My code can be located at this here:



    http://sourcepost.sytes.net/sourcevi...ource_id=15530

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Why don't you just use
    Code:
    while ( fgets( buff, sizeof buff, fp ) != NULL )
    and squash several bugs in one go.
    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
    May 2004
    Posts
    215
    ok i will do that, but can you tell me why fscanf wont work after i use strtok

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    It will - it's just you're probably
    - not understanding how fscanf works
    - you're losing white-space with %s
    - you use a lot of strcpy, where I think you meant strcat
    - you're probably overflowing some buffers.
    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.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    215
    well i checked on this website, it said it shouldnt work because strtok is not reentrant if youre using it in a loop. So i think im going to change it. Here's the site if you wanna read what they wrote.

    http://home.tiscalinet.ch/t_wolf/tw/...ing_input.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using FSCANF to read mixed text/double files
    By cfdprogrammer in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 03:18 AM

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