1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power

    Problem with reading a file as a string

    hi , this is a question in c programming.
    #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");
    while (fscanf(Math_Expression, "%s", &n) == 1)


    printf("There were %d characters in the file\n", counter);


    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 vraiable 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 .
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Rep Power
    n is a single character not an array.

IMN logo majestic logo threadwatch logo seochat tools logo