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).
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!
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]
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.
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; }
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...
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; }
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?
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; }
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...
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??
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"); }
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; }
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.
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; }
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!