txt read block by block

Hi guys, i'm having trouble on a little practice exercice i am doing for a exam, and i can find anything about it, we are not using std, i'm trying to read a .txt file block by block and i need to find the lines that only have 4 blocks and then print them to console, i've been trying alot of stuf but i can't seem to find de solution, heres the code

#include "stdafx.h"
#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LIN 3000
#define MAX_WORD 3000
#define MAX_FIELD 100

int main(int argc, char *argv[]) {
FILE *f;
char l[MAX_LIN];
char campo1[MAX_FIELD];
char campo2[MAX_FIELD];
char campo3[MAX_FIELD];
char campo4[MAX_FIELD];
char campo5[MAX_FIELD];
char campo6[MAX_FIELD];
char campo7[MAX_FIELD];
char campo8[MAX_FIELD];
char campo9[MAX_FIELD];
char campo10[MAX_FIELD];
char campo11[MAX_FIELD];
char campo12[MAX_FIELD];
char campo13[MAX_FIELD];

setlocale(LC_ALL, "Portuguese");

f = fopen(argv[1], "r");

while (fgets(l, MAX_LIN, f) != NULL)//repeats the code till END OF FILE
{

sscanf(l, "%s %s %s %s %s %s %s %s %s %s %s %s %s", campo1,
campo2, campo3, campo4, campo5, campo6, campo7, campo8, campo9, campo10, campo11, campo12, campo13); //reads the file block by block


if(campo4 == "")
fputs(l,stdout);//prints to console
}

fclose(f);

return 0;
}


Last edited on
This line is incorrect:
 
        if(campo4 == "")


To compare c-strings, use strcmp()
http://www.cplusplus.com/reference/cstring/strcmp/

 
        if (strcmp(campo4, "") != 0)



However, in the context of this problem, you should check the return value of sscanf(), which will have the number of items read.
http://www.cplusplus.com/reference/cstdio/sscanf/

1
2
    int n = sscanf( etc.
    if (n == 4)     
Last edited on
Thanks alot mate, that's exactly what i needed, btw i atually used the strcmp function, and i used it on other exercices, but i got confused whille doing this one, regarding to the sscanf function i didnt knew that it could do that i guess i need to pay more atention to the explanation on the references page.
Topic archived. No new replies allowed.