Search code examples
ctextscanfwords

fscanf() how to go in the next line?


So I have a wall of text in a file and I need to recognize some words that are between the $ sign and call them as numbers then print the modified text in another file along with what the numbers correspond to. Also lines are not defined and columns should be max 80 characters.

Ex:

I $like$ cats.  
I [1] cats.  
[1] --> like

That's what I did:

    #include <stdio.h>
    #include <stdlib.h>

    #define N 80
    #define MAX 9999
    int main()
    {
        FILE *fp;
        int i=0,count=0;
        char matr[MAX][N];

        if((fp = fopen("text.txt","r")) == NULL){
            printf("Error.");
            exit(EXIT_FAILURE);
        }

        while((fscanf(fp,"%s",matr[i])) != EOF){
            printf("%s ",matr[i]);
            if(matr[i] == '\0')
                printf("\n");

            //I was thinking maybe to find two $ but Idk how to replace the entire word     
            /*                                 
            if(matr[i] == '$')
                count++;
            if(count == 2){
                ...code...
            }
            */
            i++;
        }



        fclose(fp);
        return 0;
   }

My problem is that fscanf doesn't recognize '\0' so it doesn't go in the next line when I print the array..also I don't know how to replace $word$ with a number.


Solution

  • Not only will fscanf("%s") read one whitespace-delimited string at a time, it will also eat all whitespace between those strings, including line terminators. If you want to reproduce the input whitespace in the output, as your example suggests you do, then you need a different approach.

    Also lines are not defined and columns should be max 80 characters.

    I take that to mean the number of lines is not known in advance, and that it is acceptable to assume that no line will contain more than 80 characters (not counting any line terminator).

    When you say

    My problem is that fscanf doesn't recognize '\0' so it doesn't go in the next line when I print the array

    I suppose you're talking about this code:

            char matr[MAX][N];
                /* ... */
                if(matr[i] == '\0')
    

    Given that declaration for matr, the given condition will always evaluate to false, regardless of any other consideration. fscanf() does not factor in at all. The type of matr[i] is char[N], an array of N elements of type char. That evaluates to a pointer to the first element of the array, which pointer will never be NULL. It looks like you're trying to determine when to write a newline, but nothing remotely resembling this approach can do that.

    I suggest you start by taking @Barmar's advice to read line-by-line via fgets(). That might look like so:

    char line[N+2]; /* N + 2 leaves space for both newline and string terminator */
    
    if (fgets(line, sizeof(line), fp) != NULL) {
        /* one line read; handle it ... */
    } else {
        /* handle end-of-file or I/O error */
    }
    

    Then for each line you read, parse out the "$word$" tokens by whatever means you like, and output the needed results (everything but the $-delimited tokens verbatim; the bracket substitution number for each token). Of course, you'll need to memorialize the substitution tokens for later output. Remember to make copies of those, as the buffer will be overwritten on each read (if done as I suggest above).