I need help on this one!

This is a discussion on I need help on this one! within the C++ Programming forums, part of the General Programming Boards category; I need to know how to a move a pointer through a .txt file using. I need to move the ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    I need help on this one!

    I need to know how to a move a pointer through a .txt file using. I need to move the pointer so i can locate the beginning of a string, or an integer (or any number).
    The first rule of the Shinsen : Never dishonor the code of the Samurai.... running away from the enemy is not an option!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Have you managed to open the file? Show some code so we can see where you're up to.

    Are you wanting to load the file into memory, or work on it on disk?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    Here goes part of the code.

    if ((nota_iii<10)||(nota_iii>70))
    goto repetir_nota_iii;
    gotoxy(16,22);
    promed=alumn.llenar_alumnos(LastNameP,
    LastNameM,
    Name,
    annos, tfono,
    e_mail,
    nota_i, nota_ii,
    nota_iii);

    cout<<promed;
    alumni=fopen("c:\\alumnos.txt","w");
    fprintf(alumni,"%d) ",Pos);
    fprintf(alumni," ");
    fprintf(alumni,"%s",Name);
    fprintf(alumni," ");
    fprintf(alumni,"%s",LastNameP);
    fprintf(alumni," ");
    fprintf(alumni,"%s",LastNameM);
    fprintf(alumni," ");
    fprintf(alumni,"%d",annos);
    fprintf(alumni," ");
    fprintf(alumni,"%s",tfono);
    fprintf(alumni," ");
    fprintf(alumni,"%s",e_mail);
    fprintf(alumni," ");
    fprintf(alumni,"%d",nota_i);
    fprintf(alumni," ");
    fprintf(alumni,"%d",nota_ii);
    fprintf(alumni," ");
    fprintf(alumni,"%d",nota_iii);
    fprintf(alumni," ");
    fprintf(alumni,"%f",promed);
    fclose(alumni);

    Don't mind the functions... but here i write some things on a txt and but i can't move the pointer at will.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    fread(), and fseek() will do that for you. Assuming an open file, you might do:

    while( fread(&byte, sizeof(char), 1, flPtr) == 1) {
    //...check the value of byte here...
    }
    If you need to back-up just use fseek, which moves the internal file position indicator (which is stored in the FILE struct).

    fseek( flPtr, -1, SEEK_CURR);

    ...will move it back by one (assuming you're not already at the end of file...).

    There are ways to do this using the C++ STL, but I always prefer the C functions.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    I thank you deeply but... could you be more simple?... i may be asking too much but i new at this text thing. I don't know how fread() and fseek() really works...

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you simply want to read in lines from the file, just use fgets(). Anyway, you have to understand that all of the IO functions(fprintf, fgets, fwrite, etc.) access the FILE * you hand it, and inspect this "file position indicator", and that's how they know where to start working. So at some point you will have to learn about fseek, fwrite, fread, ftell, and rewind if you are to control to a greater extent your file access. Until then, look at this:

    Code:
    int line = 0;
    char buff[ 512 ];
    FILE * fp = fopen("user.txt", "r");
    
    
    if( fp == NULL ) {
       ///...get the hell out
     }
    
     while( (fgets(buff, 512, fp)) != NULL) {
      ++line;
      printf("Line %i:   %s", line, buff);
      getch();
     }
    
      fclose(fp);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    This code shows on screen what's on user.txt right?
    But i have one question the FILE pointer... If I have a line how do i position the pointer in the start of the second line? If i use EOF it will position the pointer in the end of the line... or not?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Again, fgets() will reposition it for you. If you're just dying to do it by hand, read in each byte and check that it's greater than 0x20 and less than 0x7F. If it is, then you are at a printable character (but not a space) and you can begin fread()'ing until you reach EOF, a newline, or a space (if applicable) and just repeat.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    I have a question... how do you assign an EOF position to a FILE pointer?...

    I tried Pointer=EOF;
    But it says that cannot assign int to a FILE...

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    I used the fseek... but it returns a 1 value... that means that the pointer hasn't been moved... HOW HOW HOW DAMNIT!! Is there another way??

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    I have serious problems here...

    Code:
    if (0==feof(ayuda))
           {
           Pos=ftell(ayuda);
           cout<<Pos; //after this it returns to 0 again... how?! WHEN!?
           }
    // Se activan las funciones del objeto Pantalla
    inicio:
           pantalla.fondo();
           pantalla.menu();
           pantalla.show_entries();
           gotoxy(64,16);cout<<LastNameP;
    
    
    // El menú
    opcion=getch();
    switch (opcion)
                   {
                    case 49:
                           {
                            cout<<Pos;
                            if(Pos==0)
                                   {
                                    fprintf(ayuda,"X");
                                    Pos++;
                                    cout<<Pos;
                                   }
                            else
                                   {
                                    Pos++;
                                    if ( fseek( ayuda, 0, SEEK_END ) != 0 )
                                          {
                                           perror( "Cannot seek" );
                                          }
                                    else {
                                          fseek(ayuda,0,SEEK_END);
                                          fprintf(ayuda,"X");
                                          alumni=fopen("c:\\alumnos.txt","w");
                                         }

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Probably because you call fseek twice:

    Code:
    if ( fseek( ayuda, 0, SEEK_END ) != 0 ) 
     {
       perror( "Cannot seek" );
     }
      else {
      fseek(ayuda,0,SEEK_END); //...get rid of this...
      fprintf(ayuda,"X");
      alumni=fopen("c:\\alumnos.txt","w");
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    NO GOOD NOTHING HAPPENED!!!

    I need a code that enters a string through keyboard and then outputs the string to a txt and the next time the program runs the next string will be written right below the other??

    I think this is asking too much but i must finish this before Monday or i'm dead.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Then just open in the append mode, ie:

    Code:
    int i = 0;
    
    while( ++1 < 11) {
    
    FILE *fp = fopen("m.txt", "a");
    
    fprintf("%s\n", str);
    
    fclose(fp);
    
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    But if i reset the program the file pointer will be in the start of the file!. How do i position de pointer in a txt file WITH INFO already in it.

    Code:
    < DAMNIT.TXT >
    
    This is a string........
     <- I want the pointer here...
    The first rule of the Shinsen : Never dishonor the code of the Samurai.... running away from the enemy is not an option!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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