Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Babkockdood

files and strings in C??

This is a discussion on files and strings in C?? within the C Programming forums, part of the General Programming Boards category; Hi I have a question regarding this code. This code is supposed to extract ASCII values from the input in ...

  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
    22,268
    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.
    myalani likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 10:28 PM.
    myalani likes this.
    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-19-2010, 12:22 AM
  2. strings in header files
    By Aisthesis in forum C++ Programming
    Replies: 28
    Last Post: 08-03-2009, 07:30 AM
  3. Searching for files using strings...
    By legit in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2009, 10: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

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