| | |
Loading a structure (text lines) from a file, modify the structure, then save
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Hey everyone. Glad to become part of this community and I hope I continue to come here for a long time seeing as this site gives great helps. My problem:
I have a struct that is (for example) the following:
I want to do the following: First I want to see if the file exists or not. Thats pretty simple using a pointer to the fopen function with the file; If it returns NULL it doesnt exist. Now the hard part is reading and loading and then after saving.
If it isnt found, all the struct will be set to the number/string 0. If its found, then all of the data from the text file is loaded onto the struct and I work with the struct modifing, deleteing, adding, etc.
At the end of the program when I hit enter, Id like for the program to save all (or the changes) to the text file so when I load the program again it has all the additions I previously made.
If Im not clear on something, go ahead and ask. Thank you very much for your help.
I have a struct that is (for example) the following:
C Syntax (Toggle Plain Text)
#define NUM 999 struct client { int id; char name[15]; char lastname[15]; int telephone; }c[NUM];
I want to do the following: First I want to see if the file exists or not. Thats pretty simple using a pointer to the fopen function with the file; If it returns NULL it doesnt exist. Now the hard part is reading and loading and then after saving.
If it isnt found, all the struct will be set to the number/string 0. If its found, then all of the data from the text file is loaded onto the struct and I work with the struct modifing, deleteing, adding, etc.
At the end of the program when I hit enter, Id like for the program to save all (or the changes) to the text file so when I load the program again it has all the additions I previously made.
If Im not clear on something, go ahead and ask. Thank you very much for your help.
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#2 May 13th, 2008
> If Im not clear on something, go ahead and ask.
What is it you want? Edward prefers to see the best in others, but if you only post the requirements of your program, at least one person is going to think you want it all written for you.
It's best to post your current attempt and ask for specific help about the code or the design. Then it's easy to see that you're just having a little trouble and not looking for a free ride.
You'll also get better help because people like Ed can focus on the trouble areas and ignore the areas that you've got covered.
What is it you want? Edward prefers to see the best in others, but if you only post the requirements of your program, at least one person is going to think you want it all written for you.
It's best to post your current attempt and ask for specific help about the code or the design. Then it's easy to see that you're just having a little trouble and not looking for a free ride.
You'll also get better help because people like Ed can focus on the trouble areas and ignore the areas that you've got covered. If at first you don't succeed, keep on sucking until you do succeed.
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#3 May 13th, 2008
So first you open it, then you read it in, parse the file in your structure, and move on to the next.
Reading in a file: fread()
Parsing: Your own mix and a bit of sscanf();
writing would be exactly the opposite: sprintf() and fwrite().
sprintf and sscanf work like printf and scanf, except the first argument is a pointer to a character array which is read from/printed to.
Reading in a file: fread()
Parsing: Your own mix and a bit of sscanf();
writing would be exactly the opposite: sprintf() and fwrite().
sprintf and sscanf work like printf and scanf, except the first argument is a pointer to a character array which is read from/printed to.
Last edited by Clockowl; May 13th, 2008 at 11:58 am. Reason: Typo's
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#4 May 13th, 2008
Id like to post the code but its in spanish so it wouldnt help much.
I tried just making a typedef with a *cl but the problem is I cant go thru each 999 of the clients (c[num]). What Id like to do is point the *cl pointer to c[num] and somehow run thru it such as *cl[num] (I know a array is already a pointer so it wont work.)
If there is any part that I could describe so someone can help me, just say.
I tried just making a typedef with a *cl but the problem is I cant go thru each 999 of the clients (c[num]). What Id like to do is point the *cl pointer to c[num] and somehow run thru it such as *cl[num] (I know a array is already a pointer so it wont work.)
If there is any part that I could describe so someone can help me, just say.
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#5 May 13th, 2008
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#6 May 13th, 2008
OK here's the code for my structs:
Ill explain:
p is a variable that I can use to access each field. It is a array so I can access all 999 possible clients in the struct (why? explanation below
As you can see I set here all values to 0. Why? Because I use 0 or "0" as a wildcard for some function to see if the client's empty or not.
The point is that I have to do all of this (add,modify,delete,etc) using a pointer that acceses a file instead of setting it values. In this file, I should have all of the fields and everytime I load the program it sets the values in the file to the struct. I work locally with the struct and when I end the program it saves all of it to a file. I have this so far:
But it doesnt work nor does it use a pointer.
Objectives:
1: Use a pointer to access the data in each field.
2: Load/save from/to a file.
C Syntax (Toggle Plain Text)
#define num 999 // Para que sean todos las estructuras de 999 /* Estructuras principales */ struct direccion { char calle[15]; //Limitado a 15 characteres int numportal; //1 digito int piso; //1 digito }; struct poblacion { char nombrepoblacion[15]; //Limitado a 15 characteres int codpostal; //Limitado a 5 digitos char provincia[15]; //Limitado a 15 characteres }; struct empleados { int id; // Limitado a una letra E seguido de 3 numeros char dni[11]; //Limitado a 8 numeros, un guion y una letra char nombre[15]; //Limitado a 15 characteres char apellido[15]; //Limitado a 15 characteres int telefono; //Limitado a 9 digitos struct direccion dir; // Una variable que es de tipo estructura dirrecion: acesso a calle, //Numportal, y piso struct poblacion pob; // Una variable que es de tipo estructura poblacion: accesso a // nombrepoblacion, codpostal, y provincia }e[num]; /* Asi podemos insertar hasta 999 productos, porque he creado la tabla que contiene num elementos de tipo empleados: Este numero es el mismo que el id */ struct clientes { int id; // Limitado a una letra C seguido de 3 numeros char dni[11]; //Limitado a 8 numeros, un espacio y una letra char nombre[15];//Limitado a 15 characteres char apellido[15];//Limitado a 15 characteres int telefono;//Limitado a 6 digitos struct direccion dir; // Una variable que es de tipo estructura dirrecion: acesso a calle, //Numportal, y piso struct poblacion pob; // Una variable que es de tipo estructura poblacion: accesso a // nombrepoblacion, codpostal, y provincia int cantcompras; // La cantidad de comprars para los descuentos }c[num]; /* Asi podemos insertar hasta 999 productos, porque he creado la tabla que contiene num elementos de tipo cliente: Este numero es el mismo que el id */ struct productos { int id; // Limitado a una letra P seguido de 3 numeros char nombre[15]; //Limitado a 15 characteres char descripcion[50]; //Limitado a 50 characteres float precio; // Precio con decimal int cantidad; //Lo que queda en stock }p[num]; /* Asi podemos insertar hasta 999 productos, porque he creado la tabla que contiene num elementos de tipo producto: Este numero es el mismo que el id */
Ill explain:
p is a variable that I can use to access each field. It is a array so I can access all 999 possible clients in the struct (why? explanation below

C Syntax (Toggle Plain Text)
void inicializar () { // Principio de funciones inicizalizar int a=0; // 1 Contador /********************* Inicializamos la estructura de clientes *************************/ /* Campos int id, char dni, char nombre, char apellido, int telefono */ for (a=0;a<999;a++) { c[a].id=0; strcpy(c[a].dni,"0"); strcpy(c[a].nombre,"0"); strcpy(c[a].apellido,"0"); c[a].telefono=0; c[a].cantcompras=0; strcpy(c[a].dir.calle,"0"); c[a].dir.numportal=0; c[a].dir.piso=0; c[a].pob.codpostal=0; strcpy(c[a].pob.nombrepoblacion,"0"); strcpy(c[a].pob.provincia,"0"); } /***************** Fin de inicializacion de clientes ******************************/ /********************* Inicializamos la estructura de empleados *************************/ /* Campos: int id, char dni, char nombre, char apellido, int telefono */ for (a=0;a<999;a++) { e[a].id=0; strcpy(e[a].dni,"0"); strcpy(e[a].nombre,"0"); strcpy(e[a].apellido,"0"); e[a].telefono=0; strcpy(e[a].dir.calle,"0"); e[a].dir.numportal=0; e[a].dir.piso=0; e[a].pob.codpostal=0; strcpy(e[a].pob.nombrepoblacion,"0"); strcpy(e[a].pob.provincia,"0"); } /********************* Fin de inicializacion de empleados ******************************/ /***************** Inicializamos la estructura de productos ***********************/ for (a=0;a<999;a++) // Este for permite que cada 999 de los productos lo ponga a 0 { p[a].id=0; strcpy(p[a].nombre,"0"); strcpy(p[a].descripcion,"0"); p[a].precio=0.0; p[a].cantidad=0; } /***************** Fin de inicializacion de productos ******************************/ } // Fin de funciones de inicializar
As you can see I set here all values to 0. Why? Because I use 0 or "0" as a wildcard for some function to see if the client's empty or not.
The point is that I have to do all of this (add,modify,delete,etc) using a pointer that acceses a file instead of setting it values. In this file, I should have all of the fields and everytime I load the program it sets the values in the file to the struct. I work locally with the struct and when I end the program it saves all of it to a file. I have this so far:
C Syntax (Toggle Plain Text)
void inicializarporarchivo() { FILE *ficheroc; FILE *ficheroe; FILE *ficherop; int i; char tid[1]; char ttelefono[9]; char tcantcompras[1]; char tnumportal[2]; char tpiso[1]; ficheroc=fopen("clientes.txt","r"); ficheroe=fopen("empleados.txt","r"); ficherop=fopen("productos.txt","r"); if (ficheroc==NULL) { void inicializar(); } /* fclose(ficheroc); ficheroc=fopen("clientes.txt","w"); for (i=0;i<999;i++) { fputs("Cliente ",ficheroc); fputc(i,ficheroc); fputs(":",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputc("0",ficheroc); fputs("\n",ficheroc); }*/ if (ficheroc!=NULL) { do { for (i=0;i<999;i++) { fgets(tid,sizeof(tid),ficheroc); c[i].id=atoi(tid); fgets(c[i].dni,sizeof(c[i].dni),ficheroc); fgets(c[i].nombre,sizeof(c[i].nombre),ficheroc); fgets(c[i].apellido,sizeof(c[i].apellido),ficheroc); fgets(ttelefono,sizeof(ttelefono),ficheroc); c[i].telefono=atoi(ttelefono); fgets(tcantcompras,sizeof(tcantcompras),ficheroc); c[i].cantcompras=atoi(tcantcompras); fgets(c[i].dir.calle,sizeof(c[i].dir.calle),ficheroc); fgets(tnumportal,sizeof(tnumportal),ficheroc); c[i].dir.numportal=atoi(tnumportal); fgets(tpiso,sizeof(tpiso),ficheroc); c[i].dir.piso=atoi(tpiso); fgets(c[i].dir.calle,sizeof(c[i].dir.calle),ficheroc); fgets(c[i].pob.nombrepoblacion,sizeof(c[i].pob.nombrepoblacion),ficheroc); fgets(c[i].pob.provincia,sizeof(c[i].pob.provincia),ficheroc); printf ("%s",c[i].dni); getch(); } putc("\0",ficheroc); }while (feof(ficheroc)!=1); } }
But it doesnt work nor does it use a pointer.
Objectives:
1: Use a pointer to access the data in each field.
2: Load/save from/to a file.
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#7 May 14th, 2008
in inicializar():
I wouldn't initialize strings like that. Better to use memset to clear them, or only set the first byte to the NULL byte. Doesn't really matter, but it makes the code a bit more readable. I think.
in inicializarporarchivo:
char tid[1];
char ttelefono[9];
char tcantcompras[1];
char tnumportal[2];
char tpiso[1];
tid, tcantcompras, tpiso: char arrays of a single char?
How do your files look? Your only using fgets, which reads in a file until it encounters a '\n'. Is that correct?
You're trying to use putc on a file which you opened as "r"eadonly. That' can't be correct.
You're testing "feof()" with "!= 1". Better would it be to continue when "feof == 0", since TRUE isn't defined as 1 but as any non-zero number.
I wouldn't initialize strings like that. Better to use memset to clear them, or only set the first byte to the NULL byte. Doesn't really matter, but it makes the code a bit more readable. I think.
in inicializarporarchivo:
char tid[1];
char ttelefono[9];
char tcantcompras[1];
char tnumportal[2];
char tpiso[1];
tid, tcantcompras, tpiso: char arrays of a single char?
How do your files look? Your only using fgets, which reads in a file until it encounters a '\n'. Is that correct?
You're trying to use putc on a file which you opened as "r"eadonly. That' can't be correct.
You're testing "feof()" with "!= 1". Better would it be to continue when "feof == 0", since TRUE isn't defined as 1 but as any non-zero number.
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#8 May 14th, 2008
•
•
•
•
in inicializar():Better to use memset to clear them, or only set the first byte to the NULL byte.
•
•
•
•
in inicializarporarchivo:
char tid[1];
char ttelefono[9];
char tcantcompras[1];
char tnumportal[2];
char tpiso[1];
tid, tcantcompras, tpiso: char arrays of a single char?
•
•
•
•
How do your files look? Your only using fgets, which reads in a file until it encounters a '\n'. Is that correct?
Client 1:
1234567-L
David
James
So each line (\n) is read and inserted into each field in the struct. Is that the correct way to do it?
•
•
•
•
You're trying to use putc on a file which you opened as "r"eadonly. That' can't be correct.
Reexplain this please.
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#9 May 14th, 2008
If you need to reopen it for writing, why don't you open it for reading and writing "rw" or reopen it with freopen()?
feof() returns a nonzero value when the FILE* has reached the EOF. Your while-loop checks if it doesn't equal one, but it should check if it equals 0: it may not return 1, it may return any nonzero number. This doesn't have to be a problem, but it's more secure.
If you haven't read anything about memset, you don't know how to initialize arrays. Which is kinda bad. You can set the whole c[], p[] and other arrays to 0 or any value you prefer with memset. Read up on it. It'd make the program more readable and faster.
By the way, here's a nice reference:
c(pp)reference
feof() returns a nonzero value when the FILE* has reached the EOF. Your while-loop checks if it doesn't equal one, but it should check if it equals 0: it may not return 1, it may return any nonzero number. This doesn't have to be a problem, but it's more secure.
If you haven't read anything about memset, you don't know how to initialize arrays. Which is kinda bad. You can set the whole c[], p[] and other arrays to 0 or any value you prefer with memset. Read up on it. It'd make the program more readable and faster.
By the way, here's a nice reference:
c(pp)reference
Last edited by Clockowl; May 14th, 2008 at 7:26 pm. Reason: Added reference URL
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Re: Loading a structure (text lines) from a file, modify the structure, then save
0
#10 May 14th, 2008
•
•
•
•
If you need to reopen it for writing, why don't you open it for reading and writing "rw" or reopen it with freopen()?
Don't know about freopen.
•
•
•
•
feof() returns a nonzero value when the FILE* has reached the EOF. Your while-loop checks if it doesn't equal one, but it should check if it equals 0: it may not return 1, it may return any nonzero number. This doesn't have to be a problem, but it's more secure.

•
•
•
•
If you haven't read anything about memset, you don't know how to initialize arrays. Which is kinda bad. You can set the whole c[], p[] and other arrays to 0 or any value you prefer with memset. Read up on it. It'd make the program more readable and faster.
[/quote]
Thanks.
Thanks BTW for all your help Clockowl.

I have another question but related.
As you see I have
c[num] for my structs.
How can I use a pointer to go thru all 999 positions for the struct? Pointers and file handling are my weakpoints; Any more pages about these subjects would be great. Again thanks Clockowl
![]() |
Other Threads in the C Forum
- Previous Thread: Debug assertion failed problem
- Next Thread: Horse Game
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





