Thread: how to read a file as a string

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    how to read a file as a string

    Hi , this is a question in c programming.
    Code:
    #include <stdio.h>
    int main(void)
    {
      FILE *Math_Expression;
      int counter = 0;
      int total = 0;
      float average;
      char n;
      Math_Expression = fopen("pf1.txt", "r");
      if (Math_Expression == NULL) {
        printf("Can't open numbers.txt\n");
        exit(1);
      }
      while (fscanf(Math_Expression, "%s", &n) == 1) {
        printf("%s", n);
        counter++;
      }
    
    
      fclose(Math_Expression);
    
      printf("There were %d characters in the file\n", counter);
    
      ;
    
      system("pause");
      return 0;
    }
    The code above , doesnt compile.
    If i change the "%s " to "*c" it works; why is that ?
    Also the real thing i need to do is to be able to read in values from the file as string , and then play around with the values depending on their type ( operator like *,/,+,- etc or if its a decimal ) . The problem i face is if i need to read in the values as "%s", i will be stuck fiddling with a variable with an "&" sign .
    How do i read in the values of the files regardles of it being an operator or a number , and then check the string for its type ( *,+,-,3.2).
    I ran into trouble trying to store the values of the array as a string, i got a an error when i used the strcpy function to store the values from the file into a string , strcpy ( char, &n) ..where &n is the current value read from the file . The error was " [Warning] passing arg 1 of `strcpy' makes pointer from integer without a cast ".
    Also how do i get rid of the & ( pointer adress) ?
    As i have to extract values from the file as a string and then operate on them , as in compare them or add them or subtract them etc .; how do i do do these without getting the same "makes pointer without a cast error ".

    Please help me as soon as possible.

    Any feedback will be appreciated xD .
    Last edited by Salem; 12-22-2012 at 06:00 AM. Reason: removed font abuse

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First step, compile with warnings.
    Code:
    $ gcc -Wextra -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:12:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    foo.c:12:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    foo.c:15:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:26:3: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
    foo.c:7:9: warning: unused variable ‘average’ [-Wunused-variable]
    foo.c:6:7: warning: unused variable ‘total’ [-Wunused-variable]
    > If i change the "%s " to "*c" it works; why is that ?
    You've got the whole char-vs-string thing mixed up.

    If you have
    char n;
    then use
    fscanf(Math_Expression, "%c", &n);
    printf("%c", n);


    If you have
    char n[100];
    then use
    fscanf(Math_Expression, "%s", n);
    printf("%s", n);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read string from half way through a file?
    By aadil7 in forum C Programming
    Replies: 9
    Last Post: 01-10-2011, 08:23 PM
  2. Cannot read string from a file!
    By adrian2009 in forum C Programming
    Replies: 7
    Last Post: 02-28-2009, 04:41 AM
  3. read string from file until EOF
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 09:33 AM
  4. Read Int & String from a File
    By tim_messer in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2002, 04:59 PM
  5. Replies: 1
    Last Post: 02-12-2002, 09:25 AM