Problem reading from a file..

This is a discussion on Problem reading from a file.. within the C Programming forums, part of the General Programming Boards category; i have a function that is going to read from a file, first look for the specific check number the ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Unhappy Problem reading from a file..

    i have a function that is going to read from a file, first look for the specific check number the user is trying to see, then print it to the screen, i got it to work by just taking all the data, until "-" shows up, the break point between each record, but i need to divide everyline into a variable so i can put everything very fancy , the only problem is that im not pretty good a C, and i've try many things, gone thru several forums, i don't have a book, and i need to turn this in soon.

    thanks in advance, here goes the function;

    Code:
    void mostrarcheque()
    {
        system ("cls");
        printf("\n%-27cImpresion de Cheques\n\r");
        printf("\n%-28cMostrar Cheques\n\r");
        FILE *file;    
        file = fopen("cheques.txt", "r"); 
        struct cheque micheque;
        char buffer[255];
        char buffer_numerocheque[3];
        int key = 0;
        printf("\n%-7cBuscar por: \n\r"); 
        printf("\n%-7cEscriba el Cheque : ");
        fgets (buffer_numerocheque,255,stdin);
        if(file==NULL) 
        {
            printf("Error: No se pudo abrir el archivo.\n");
        }
        else
        {
            while(fgets(buffer, 255, file)!= NULL) 
            { 
                    if ( strcmp (buffer, buffer_numerocheque) == 0 )
                    {
                         printf("\n********************************************************************************\n\n\r");
                         printf("\n%-60cCheque No.");
                         do
                         {
                         buffer==micheque.numerodecheque;                      
                         printf("%i\n", micheque.numerodecheque);
                         fgets(buffer, 255, file);
                         buffer==micheque.destinatario;
                         fgets(buffer, 255, file);
                         buffer==micheque.fecha;
                         fgets(buffer, 255, file);
                         buffer==micheque.nocuenta;
                         fgets(buffer, 255, file);
                         buffer==micheque.banco;
                         fgets(buffer, 255, file);
                         buffer==micheque.monto;
                         } while ( buffer[0] != '-' );
                         break;
                    }
            }
        }
    
        printf("\n\n%-50c____________________________ \n\r");
        printf("\n%-60cFirma \n\r");    
        printf("\n********************************************************************************\n\n\r");
        continuar(); 
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >but i need to divide everyline into a variable so i can put everything very fancy
    what do u mean how to get each fields from the line read from the file. u might well use sscanf or strtok. but i recommand u not to use strtok it changes the original string. better use sscanf

    NOTE: is your code u are not checking the return value of fgets. u must check that.

    ssharish2005

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Talking

    Okey i got improvement

    few problems:

    1st. i goes thru the do-while again and then it wud show again every field with - and then stop.
    2nd. sscanf doesnt takes spaces? for example on the name i had "Erick Valverde" it just takes "Erick"

    Code:
    void mostrarcheque()
    {
        system ("cls");
        printf("\n%-27cImpresion de Cheques\n\r");
        printf("\n%-28cMostrar Cheques\n\r");
        FILE *file;    
        file = fopen("cheques.txt", "r"); 
        struct cheque micheque;
        char buffer[100];
        char buffer_numerocheque[3];
        int key = 0;
        printf("\n%-7cBuscar por: \n\r"); 
        printf("\n%-7cEscriba el Cheque : ");
        fgets (buffer_numerocheque,255,stdin);
        if(file==NULL) 
        {
            printf("Error: No se pudo abrir el archivo.\n");
        }
        else
        {
            while(fgets(buffer, 255, file)!= NULL) 
            { 
                    if ( strcmp (buffer, buffer_numerocheque) == 0 )
                    {
                         printf("\n********************************************************************************\n\n\r");
                         printf("%-60cCheque No.");
                         do
                         {
                                               
                         sscanf(buffer, "%s", micheque.numerodecheque);         
                         printf("%s\n", micheque.numerodecheque);
                         fgets(buffer, 255, file);
                         sscanf(buffer, "%s ", micheque.destinatario);         
                         printf("Paguese a : %s\n", micheque.destinatario);
                         fgets(buffer, 255, file);
                         sscanf(buffer, "%s ", micheque.fecha);         
                         printf("Fecha : %s\n", micheque.fecha);
                         fgets(buffer, 255, file);
                         sscanf(buffer, "%s ", micheque.nocuenta);         
                         printf("Numero de Cuenta : %s\n", micheque.nocuenta);
                         fgets(buffer, 255, file);
                         sscanf(buffer, "%s ", micheque.banco);         
                         printf("Banco : %s\n", micheque.banco);
                         fgets(buffer, 255, file);
                         sscanf(buffer, "%s ", micheque.monto);         
                         printf("Monto Total : %s\n", micheque.monto);
                         buffer=="-";
                         } while ( buffer[0] != '-' );
                         break;
                    }
            }
        }
    
        printf("\n\n%-50c____________________________ \n\r");
        printf("\n%-60cFirma \n\r");    
        printf("\n********************************************************************************\n\n\r");
        continuar(); 
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    3


    okey, i got my wait out of the do-while by using

    Code:
    buffer[0]='-';
    so 1st problem solved. any help on the second?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > printf("\n%-28cMostrar Cheques\n\r");
    > FILE *file;
    Is this C or C++?
    Normal standard C doesn't permit mixing declarations and statements within the same block.
    This is a feature introduced in C99

    > printf("\n%-27cImpresion de Cheques\n\r");
    1. What is that % conversion doing at the start, and where are the conversion parameters?
    2. There is no need for a \r at the end of all your printf statements.

    > fgets (buffer_numerocheque,255,stdin);
    I see you've misunderstood the nature of the 2nd parameter to fgets()
    It's the size of the array you're storing data in, not the amount you expect the user to type.
    Your buffer_numerocheque is only 3 bytes, after which you start trashing someone elses memory.

    Try
    fgets (buffer_numerocheque,sizeof buffer_numerocheque, stdin);
    and make the buffer larger as well.

    > while(fgets(buffer, 255, file)!= NULL)
    Again, sizeof buffer

    > if ( strcmp (buffer, buffer_numerocheque)
    Bear in mind that both these buffers will have a \n at the end as returned by fgets().

    > buffer=="-";
    This does nothing.

    How about
    Code:
    if ( strcmp (buffer, buffer_numerocheque) == 0 ) {
      // your code
    } else
    if ( buffer[0] == '-' ) {
     break;
    }
    > cheques.txt
    An example of a few lines of data from this file would really help sort out what set of fgets() and sscanf() calls you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Replies: 20
    Last Post: 06-13-2005, 12:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21