[Dev-C++] FILE FUNCTIONS and their problems
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <cm...@ya...> - 2004-05-06 04:58:20
|
Im having some problems with the program that is
attached to this mail, I have some comcept of some
files functions and they do not work as I thought they
would.
Let me explain what my program should do: I was asked
by a teacher to do a program that can insert and erase
variable length registers(I dont know if this is the
rigth way to explain it, it means that the registers
are equally in a matter of space), so i decided to
make a file that stores names of people (variable) and
their respectives ages right?
So i put the differents procedures to make the needed
work, one to insert the register, one to show all the
registers on the file, and one to eliminate a
determined register. By the way i used the following
file functions fopen, fseek, fread, fwrite.
INSERT AND SHOW(agregar y mostrar)
This part is a little hard for me to explain it, i
hope you can understand me, (and help me!!!!) I
decided to received the name trought the keyboard
input and then take the quantity of characters in the
string and then receive the age. All in that order are
inserted in a file. Which means that in a file with
one register, it would only have one integer (number
of characters in the string)+ the previous integer *
characters + the age integer. So I said im going to
store a register and Im going to show the file so i
can see that that register was saved right.
Ok let me show you part of the insert code and explain
my main problem or doubt
while(!feof(fd)){
fread(&cant,sizeof(int),1,fd); // quantity of
characters
char f=getchar();
printf("\nEl numero de caracteres es %d\n",cant);
if(cant<0){ // When the program eliminates
registers, it inserts the quantity of characters but
negative
fseek(fd,(-1*cant)+2,1); // so the position of the
next register can be calculated
}
else{
nom=(char*)malloc(cant);
fread(nom,sizeof(char)*cant,1,fd);
fread(&ed,sizeof(int),1,fd);
printf("%d:",cont++);
printf("El señor %s tiene %d años",nom,ed);
cant=0;
}
}
This was my first try and as you can see the fread
function is right after the while but when I ran the
program I inserted only one register and when I asked
the program to show it to me it showed me two but the
second had no name. So just to try I decided to put
the fread after the while and again after the cant=0
next to the end of the while (the same right)
NOOOOO!!!! The problem was aparently soluted and this
means that some garbage must be saved on the file and
i dont know where. Other problem was that I tried to
count how many bytes of garbage i had after the age
integer so i tried to put the next code after the
cant=0
Int counter=1;
While(¡feof(fd)){
Printf(\n%d,counter);
Fseek(fd,1,1);
counter++;
}
so I could see this information. But aparently the
fseek did not move file pointer at all because it
enter in an infinite loop and it didnot finished.
One last question will be where can i get
documentation about the different functions of C/C++,
some thing that explains every standard function.
Thanks for reading my mail.
PLEASE EXCUSE MY ENGLISH IM NOT VERY GOOD AT IT.
Claudio Veas
------------
Los mejores usados y las más tentadoras
ofertas de 0km están en Yahoo! Autos.
Comprá o vendé tu auto en
http://autos.yahoo.com.ar |