Thread: files and strings in C??

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    files and strings in C??

    Hi

    I have a question regarding this code. This code is supposed to extract ASCII values from the input in the file testme.txt (which only contains: KMN) and then convert every character to ASCII (K = 75. M = 77, etc..) but for some reason the code only converts the first character.
    I want to use fscanf only.

    Code:
    #include <stdio.h>
    int main(void){
        char ac;
        FILE *inp, *outp;
        inp = fopen("testme.txt","r");
        outp = fopen("testme.out","w");
        fscanf(inp,"%c",&ac);
        printf("The number read from the file is %d.\n",ac);
        fprintf(outp,"ASCII was %d.\n",ac);
        fclose(inp);
        fclose(outp);
        return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The reason is that you only read the first character

    You could read the characters in a loop instead, ending when EOF is reached. Since you don't appear to have any special formatting requirements, it probably makes more sense to use fgetc instead of fscanf for this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    It only reads the first value because you only tell it to read the first value, fscanf is only called once. You need to use a loop.

    Code:
    inp = fopen("testme.txt", "r");
    outp = fopen("testme.out", "w");
    int x;
    while ((x = fgetc(inp)) != EOF) {
        printf("%d ", x);
        fprintf(outp, "%d ", x);
    }
    printf("\n");
    fprintf(outp, "\n");
    fclose(inp);
    fclose(outp);

    fgetc() returns a character from a file stream. In the context in which you're only looking for character input, you should use fgetc() instead of fscanf(). fscanf() works, but fgetc() is more efficient. Since I put the x assignment inside the while loop's conditional, x is assigned once every time the loop repeats, and fgetc will return the next character in the stream. The while loop is essentially saying:

    Code:
    read a character
    if character is not the end-of-file indicator (EOF),
        print character in decimal form to stdout
        print character in decimal form to outp
    Read this for more info: For, While and Do While Loops in C - Cprogramming.com
    Last edited by Babkockdood; 10-09-2011 at 09:28 PM.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help copying files into strings???
    By patso in forum C Programming
    Replies: 5
    Last Post: 08-18-2010, 11:22 PM
  2. strings in header files
    By Aisthesis in forum C++ Programming
    Replies: 28
    Last Post: 08-03-2009, 06:30 AM
  3. Searching for files using strings...
    By legit in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2009, 09:00 AM
  4. files, strings, comparation ...
    By milan in forum C Programming
    Replies: 4
    Last Post: 12-19-2002, 09:26 AM
  5. Help...using strings and txt files
    By Juicehead in forum C Programming
    Replies: 0
    Last Post: 11-26-2002, 09:01 AM