How to compare the first string from last line in a file.txt 'C'

Pages: 12
But depending upon the file format, that could be an issue. Please post an exact example of the file used.

The posted code works with the data as posted in the first post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE* inpf = fopen("file.txt", "r");

	if (inpf == NULL) {
		puts("Cannot open input file");
		return 1;
	}

	int id = 0;

	for (int dum = 0; fscanf(inpf, "%d %d %d %d", &id, &dum, &dum, &dum) != EOF; );

	printf("Last is %d\n", id);
}


when file.txt contains:


22310 01 09 2020
22500 01 10 2020
22700 01 11 2020


displays as expected:


Last is 22700


As your data file has a different format, I suggest first that this code is modified to work with the actual data file. When it does, then it can be incorporated into the main program.
Try this. It is an alternative way of parsing the data. The format following the id now doesn't matter. If the length of the lines are greater than 255 chars, the size of the buffer etc will need changing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE* inpf = fopen("file.txt", "r");

	if (inpf == NULL) {
		puts("Cannot open input file");
		return 1;
	}

	char id[6] = {0};

	for (char buffer[256] = {0}; fscanf(inpf, "%5s", id) != EOF && fgets(buffer, 255, inpf) != NULL; );

	printf("Last is %s\n", id);
}



With the file format:


22310    |   01   |  09 |    2020     |  30
22500    |   01   |  10 |    2020     |  40
22700    |   01   !  11 !    2020     |  50 


displays as expected:


Last is 22700

Well I think I have a problem with my code that doesn't parse the data correctly.. I changed in my code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void adaugaInfoApa(struct INFO* info, struct TIMP* timp)
{
..
...
char tmpApa[6] = {0};

                for (char ln[MAX] = {0}; fscanf(file, "%5s", tmpApa) != EOF && fgets(ln, 999, file) != NULL; );

                info->diferenta = diferentaIndex(tmpApa, temp);

                fprintf(file, "%s    |   %s   |   %s   | %s |  %ld \n", temp, timp->zi, timp->luna, timp->an, info->diferenta);
...
..
}


then the result isn't the one expected...
My true goal here is to parse the char tmpApa to this function:

1
2
3
4
5
6
7
8
9
10
11
int diferentaIndex(char index1[MAX], char index2[MAX])
{
    long int rezultat;
    long int x, y;

    x = atoi(index1);
    y = atoi(index2);

    rezultat = y - x;

    return rezultat;


But seems the "x" which has to be the char index1, which passing to the function adaugaInfoApa(struct INFO* info, struct TIMP* timp) where it has to be char tmpApa is 0. Parsing the data to the int diferentaIndex(char index1[MAX], char index2[MAX]), has to do an operation which is
subtraction
, but what I got after the first executing the code is this:

1
2
11898    |   01   |  09 |    2020     |  0
12000    |   01   |  10 |    2020     |  12000  <- the result has to be 102


Well.. 12000 - 11898 will not be ever = 12000, that means the char tmpApa was 0 at that moment and I don't know what is the real problem.

I tried your example in an fresh project and is working in all formats.. so I guess I'm having a problem with my other function which is not working properly.. atoi() function isn't suppose to convert the string argument I mean char to an int integer? Then I can do whatever oeration I want with the arguments, or not ???
Last edited on
Is MAX 1000? tmpApa is a char array of only 6. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
int diferentaIndex(const char* index1, const char* index2)
{
    int rezultat;
    int x, y;

    x = atoi(index1);
    y = atoi(index2);

    rezultat = y - x;

    return rezultat;
}


Note type changes for x, y. atoi() returns a type int. If you want a type long int, use atol().

If this doesn't work, put some printf() statements in diferentaIndex() to see the contents of index1, index2, the values of x and y (or use the debugger if available to see the contents).
Yes seeplus MAX, Idefined as 1000.

Changed now to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int diferentaIndex(const char* index1, const char* index2)
{
    long int rezultat;
    long int x, y;

    x = atol(index1);
    y = atol(index2);

    rezultat = y - x;

    printf("diferenta =  %ld", rezultat);

    return rezultat;
}


Guess what ?! the result is 12000. So the function int diferentaIndex is working, but the data from char tmpApa is not parsing as it should be..
I'm work on it.. damn I have like more than one day I am at the same point.
Okay I think I solve it..

The mistake was here.. I mean not a mistake, but something that is missing in the adaugaInfoApa function, and now is working fine... so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void adaugaInfoApa(struct INFO* info, struct TIMP* timp)
{
..
...
char tmpApa[6] = {0};

rewind(file); //-> this was missing..

                for (char ln[MAX] = {0}; fscanf(file, "%5s", tmpApa) != EOF && fgets(ln, 999, file) != NULL; );

                info->diferenta = diferentaIndex(tmpApa, temp);

                fprintf(file, "%s    |   %s   |   %s   | %s |  %ld \n", temp, timp->zi, timp->luna, timp->an, info->diferenta);
...
..
}


Of course I had to set the pointer to the begining of the file before the loop.

seeplus However I have to thank you ... not many ppl. keep the calm and walk to the end with a beginner, so I guess I owe you..
Until the next time Cheers :D
Last edited on
If diferentaInde() is working, then I suggest you use the debugger to trace through the code and watch the contents of the variables as the code progresses. If you don't have access to a debugger, put printf() statements at key points to display variables' contents and to act as a simple tracer.
Ouu I guess you replay after I replay so.. just to know, I solve the problem. Was just a rewind(file) before the for loop. So after setting the file pointer to the beginning of the file the data is parsing like expected do with other words now is working perfectly. I replay before you did so you can take a look on my post again.
Thank you again man .. :D
Registered users can post here. Sign in or register to post.
Pages: 12