Parse file TXT in C

0

The exercise consists of given a TXT file created with the notebook that the program read and parse in a variable way, detecting each field of the text file and recognizing them so that they can be used or displayed. My problem is that the while skips as if the file did not exist but does exist. I thank you for your response

Main:

#include"prototipos.h"

int main()
{
    FILE *archtxt;


    archtxt= fopen("empleados.txt","r+t");

    if(!archtxt)
    {
        printf(" no se pudo acceder al archivo");
        return 1;
    }

    txt_a_parsear(archtxt);

    fclose(archtxt);

    return 0;
}

Header :

#include"prototipos.h"

int main()
{
    FILE *archtxt;


    archtxt= fopen("empleados.txt","r+t");

    if(!archtxt)
    {
        printf(" no se pudo acceder al archivo");
        return 1;
    }

    txt_a_parsear(archtxt);

    fclose(archtxt);

    return 0;
}

Functions.c :

#include"prototipos.h"

void txt_a_parsear(FILE *txt)
{
    t_empleado emp;
    char linea[TAM];

    fgets(linea,TAM,txt);

    while(!feof(txt))
    {
        parseo_txt_var(linea,&emp);
        fgets(linea,TAM,txt);

    }
}

void parseo_txt_var(char * linea,t_empleado *emp)
{

    char *act = strchr(linea,'\n');

    *act='
#include"prototipos.h"

int main()
{
    FILE *archtxt;


    archtxt= fopen("empleados.txt","r+t");

    if(!archtxt)
    {
        printf(" no se pudo acceder al archivo");
        return 1;
    }

    txt_a_parsear(archtxt);

    fclose(archtxt);

    return 0;
}
'; act=strrchr(linea,'|'); emp->sexo=*(act+1); *act='
#include"prototipos.h"

int main()
{
    FILE *archtxt;


    archtxt= fopen("empleados.txt","r+t");

    if(!archtxt)
    {
        printf(" no se pudo acceder al archivo");
        return 1;
    }

    txt_a_parsear(archtxt);

    fclose(archtxt);

    return 0;
}
'; act=strrchr(linea,'|'); sscanf(act+1,"%f",&emp->sueldo); *act='
#include"prototipos.h"

void txt_a_parsear(FILE *txt)
{
    t_empleado emp;
    char linea[TAM];

    fgets(linea,TAM,txt);

    while(!feof(txt))
    {
        parseo_txt_var(linea,&emp);
        fgets(linea,TAM,txt);

    }
}

void parseo_txt_var(char * linea,t_empleado *emp)
{

    char *act = strchr(linea,'\n');

    *act='%pre%';
    act=strrchr(linea,'|');

    emp->sexo=*(act+1);

    *act='%pre%';
    act=strrchr(linea,'|');
    sscanf(act+1,"%f",&emp->sueldo);

    *act='%pre%';
    act=strrchr(linea,'|');

    sscanf(act+1,"%d/%d/%d",&emp->fnac.dia,&emp->fnac.mes,&emp->fnac.ano);

    *act='%pre%';
    act=strrchr(linea,'|');

    strncpy(emp->apyn,act+1,sizeof(emp->apyn));

    *act='%pre%';


     sscanf(linea,"%d",&emp->dni);
}
'; act=strrchr(linea,'|'); sscanf(act+1,"%d/%d/%d",&emp->fnac.dia,&emp->fnac.mes,&emp->fnac.ano); *act='%pre%'; act=strrchr(linea,'|'); strncpy(emp->apyn,act+1,sizeof(emp->apyn)); *act='%pre%'; sscanf(linea,"%d",&emp->dni); }
    
asked by Luciano Pulido 04.01.2017 в 19:06
source

2 answers

0

You may be invoking undefined behavior so anything could happen (work, not work, malfunction, or < a href="http://catb.org/jargon/html/N/nasal-demons.html"> invoke demons that would be fired from your nostrils .

As far as I can see you are using an extended reading mode ( "r+" ) when opening the file at the same time that you request text mode ( "t" ). However the text mode I do not see it referenced in any documentation of fopen :

The only documentation referring to the "t" mode is on cplusplus.com and written in passing as unofficial by any compiler (translation and highlighted by me):

  

If there are additional characters in the sequence, the behavior depends on the implementation of the library : some implementations may ignore the additional characters so that for example a "t" additional (sometimes used for explicit text mode) is accepted.

If we consult the C standard we see the following modes accepted (translation and highlighted mine):

  

7.19.5.3 the function% co_of%

     

...

     
  • The fopen argument points to a string of characters. If the string points to one of the following, the file opens in the indicated mode. Otherwise the behavior is undefined :      
    • mode open a file for reading
    •   
    • r truncates to zero or creates a text file for writing
    •   
    • w add; open or create a text file to write to the end of the file
    •   
    • a opens in binary mode for reading
    •   
    • rb truncates to zero or creates a file in binary mode to write to the end of the file
    •   
    • wb add; open or create a file in binary mode to write to the end of the file
    •   
    • ab opens a text file to update (read and write)
    •   
    • r+ truncates to zero or creates a text file to update
    •   
    • w+ add; open or create a text file to update, the write pointer is placed at the end of the file
    •   
  •   

    So it's possible that it does not work for you in the a+ mode.

        
    answered by 05.01.2017 в 08:10
    0

    turn off the notebook. I caught it later and the program worked correctly. entry to the while and parsing the file. so I had nothing wrong greetings

        
    answered by 06.01.2017 в 04:37