fscanf problems

This is a discussion on fscanf problems within the C Programming forums, part of the General Programming Boards category; Hi, this is a simple task but i've been breaking my head over it for a day and can't figure ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    fscanf problems

    Hi, this is a simple task but i've been breaking my head over it for a day and can't figure out what the problem is. Any help would be good.
    Code:
    #include <stdlib.h>
    # include <string.h>
    #include <stdio.h>
    #include <stdint.h>
    
    int main(){
    
    char key[10];
    strcpy(key, " ");
    FILE *pFile = fopen("testing", "a");
    
    fprintf(pFile, "HELLO WORLD");
    
    rewind(pFile);
    int i = fscanf(pFile,key);
    if (i=EOF){
              printf("EOOOOOOOOF\n");
              }
    
    printf("%s\n", key);
    
    fclose(pFile);
    
    
    return 0;
    
    }
    Why is this printing EOOOOF then blank? I just want a simple program that reads from a file and stores a string without white spaces into a string. This is a simple task but for some reason it's always scanning from the end of the file therefore returning blank spaces into the string. Thank You for any help.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    For one, you are trying to read from a file that was opened for appending. Try:
    Code:
    FILE *pFile = fopen("testing", "a+");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  3. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM
  5. fscanf problems
    By QuincyEQ in forum C Programming
    Replies: 2
    Last Post: 07-29-2002, 04:24 PM

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