Home > Back-end >  Read a particular line from file and `strcmp` the character from that line fails in c code
Read a particular line from file and `strcmp` the character from that line fails in c code

Time:03-08

I thought to not to ask this simple question about file handing, but I am not getting why I Am getting this error.

I have a input file.txt which has dump from the code run -

**** input.txt ****
frame_number:0 | x:541 ## y:8 @idx:0
frame_number:1 | x:51 ## y:8 @idx:0
...
frame_number:50 | x:579 ## y:18 @idx:1
...
frame_number:100 | x:593 ## y:21 @idx:2
...
frame_number:1000 | x:613 ## y:10 @idx:3
...

I have written a code for reading the same content and wants to read frame number, x and y coordinates for every index.

#include <stdio.h>
#include <stdlib.h> // For exit()
#include <string.h>


static const char* input_cornersfilename     = "input.txt" ;
static const char* output_filename           = "output.txt" ;

int readFrameNumber(char *line)
{
    char frame_number[4];
    char readContent[6];
    strncpy(readContent, line 13, 6);

    int i = 0;
    int result;

    while(i < 6) {

        result = strcmp(&readContent[i], "|");

        if(result !=0) {

            frame_number[i] = readContent[i];

        } else {

            break;

        }

        i  ;
    }

    return atoi(frame_number);
}

int readXCord(char *line)
{
    char xCord[4];
    char readContent1[15]; //= "500 | x : 541 ## y:8 @idx:0";
    strncpy(readContent1, line 13, 14);

    int i = 0;
    int result;

    char cmpchar[] = "x";

    while(i < 14) {

        result = strcmp(&readContent1[i], cmpchar);

        if(result == 0) {
            strcpy(xCord, &readContent1[i]);
            xCord[i] = readContent1[i];

        } else {
            printf("i;%d | readContent[i];%c\n", i, readContent1[i]);
            //break;

        }

        i  ;
    }

    return atoi(xCord);
}

int readYCord(char *line)
{
    char yCord[4];
    char readContent1[15]; //= "500 | x : 541 ## y:8 @idx:0";
    strncpy(readContent1, line 13, 20);

    int i = 0;
    int result;

    char cmpchar[] = "y";

    while(i < 14) {

        result = strcmp(&readContent1[i], cmpchar);

        if(result == 0) {
            strcpy(xCord, &readContent1[i]);
            xCord[i] = readContent1[i];

        } else {
            printf("i;%d | readContent[i];%c\n", i, readContent1[i]);
            //break;

        }

        i  ;
    }

    return atoi(yCord);
}

int main()
{
    FILE *fptr1, *fptr2;
    char c;

    // Open one file for reading
    fptr1 = fopen(input_cornersfilename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", input_cornersfilename);
        exit(0);
    }

    // Open another file for writing
    fptr2 = fopen(output_filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", output_filename);
        exit(0);
    }

    int frameIdx = 0, x_cord = 0, y_cord = 0;
    char frame_number[40];
    char line[256]; /* or other suitable maximum line size */
    while (fgets(line, sizeof(line), fptr1) != NULL) /* read a line */
    {
        printf("Line read:%s", line);

        frameIdx = readFrameNumber(line);
        x_cord = readXCord(line);
        y_cord = readYCord(line);

        printf("frame_number:%d | x_cord:%d | y_cord:%d \n", frameIdx, x_cord, y_cord);
    }

    printf("\nContents copied to %s", input_cornersfilename);

    fclose(fptr1);
    fclose(fptr2);
    return 0;
}

Question:

  1. Here my strcmp is not comparing the the string with the required character strings and My code is not doing as per the expectation.

  2. Can any one suggest me some other method to read frame_number, x and y coordinates from this input.txt

Can any one tell me my mistake in my code.

CodePudding user response:

There are several problems here: You fail to add a terminating null character to the strings frame_number, xCord and yCord; you never write anything to your output file; and you've made the entire process unnecessarily complicated. You can completely eliminate the functions readFrameNumer, readXCord, and readYCord, and make the loop in main look like this:

while (fgets(line, sizeof(line), fptr1) != NULL) /* read a line */
{
    printf("Line read:%s", line);
    if (sscanf(line, "frame_number:%d | x:%d ## y:%d",
            &frameIdx, &x_cord, &y_cord) != 3) {
        /* handle error */
        exit(1);
    }
    printf("frame_number:%d | x_cord:%d | y_cord:%d \n",
            frameIdx, x_cord, y_cord);
}
  •  Tags:  
  • c
  • Related