• 0

[C] Loading a structure from a file, modify the structure, then save


Question

I have a local structure with several fields. I want to make this local structure be stored in a file. Everytime I load up the program it can either A: Create the file with the structure if it doesnt exist B: Load everything from the file into each structure's field. Then I work in the program modifying it, deleting it, etc etc. from the local structure then when I exit the program it has to save everything to the same file.

If someone needs code samples, Ill put them but please help me out as Im having problems opening/closing...

23 answers to this question

Recommended Posts

  • 0

I've actually tried this for some uni project, but I could never get it to work =/

If i still have a copy of the cource code somewhere, i'll post it here

---

You could post your code and I could try to get it to work, maybe it was different from mine

  • 0

I have

struct clients

{

int id;

char name[15];

char last[15];/

int number;

}c[num];

And I want to do the following:

I want to (this is seperte but related) control the information with a pointer.

And (this thread) insert id, name, last, number into the struct that at the begin and end of the program will load and save it from a clients.txt

And Im simply not sure how to do the second :) So if someone could help me out thanks.

OT: Is there any good and dedicated community with forums to programming, overall C?

  • 0
I have...

FILE *f=fopen(argv[1], "r");
list *l1; int i;
l1 = makelist(f);
fclose(f);

list *makelist(FILE *stream){
char letter;
int i = 0; int icount =0;
char word[30];
list *l1;
l1= NULL;
/*creates a new list and initialises it. While the character retrieved from//
the stream is not the end of the file,*/
while((letter=fgetc(stream))!=EOF){
/*initialises the character array to put the word on*/
for(icount=0; icount<30; icount++){
word[0] = '\0';
}
/*checks to see that the character is a letter*/
	if(((letter>64&&letter<91)||(letter>96&&letter<123)||letter==32)){
		if ((letter>64&&letter<91)||(letter>96&&letter<123)){
			while(letter!=EOF && (letter!=32) && (letter!='\0') && (letter!='\n')&& (letter!='.') && (letter!=',') && (letter!='\'')&&(letter!='"')&&(letter!=';')&&(letter!=':')&&(letter!=39)&&(letter!='!')&&(letter!='?')&&(letter!=39)&&(letter!=40)&&(letter!=41)&&(letter!=48)&&(letter!=49)&&(letter!=' ')&&(letter!='(')&&(letter!=')')){
				word[i] = letter;
				i++;
				letter=fgetc(stream);
		}
			/*adds an end marker on the end of the string*/
			word[i] = '\0';
			i = 0;
			/*makes the word uppercase*/
			for (i=0; i<strlen(word); i++){
				word[i] =toupper(word[i]);
			}
			i =0;
			/*places the word on the list*/
			l1 = insertlist(word, l1);

		}else{
			i= 0;
		}
	}	
}
/*returns the new list with the item added*/
return l1;
}

That's some code for reading to a list that I ripped from somtehing I did in my first year of uni :s.

As for writing... I did that somewhere too I'll look for a code fragment you could use.

Chris

[edit] Just read the if conditions; lol... what was going on there... I think there was an odd bug in it for a while, you can ignore most of them!!! [/edit]

  • 0

WOW :) You accually kind of help me out on another project!!!

I also have to write/read to graphs, lists, trees, and nodes. Thanks alot.

I understand all of the lines except why you use some of the variables and

while((letter=fgetc(stream))!=EOF){

What is fgetc exactly? (Something about a file and a character but...)

If you could help me out, or If i could add you to my Messenger if you dont mind, thank you very very VERY much :)

  • 0
I understand all of the lines except why you use some of the variables and

while((letter=fgetc(stream))!=EOF){

What is fgetc exactly? (Something about a file and a character but...)

I'm no expert with C but I'm pretty sure all that line does, is to look reading all characters from a stream until it encounters the EOF (End of File). fgetc just gets a character from a stream. More info here :)

  • 0
What exactly is a "stream"? I know its a noob question but...

You can think of a stream as a file. If you close the stream, you close the file. There are three special streams in C - stdin (standard input stream), stdout (standard output stream) and stderr (standard error output stream). Usually those are associated with the first three file handles:

stdin = file handle 0

stdout = file handle 1

stderr = file handle 2

  • 0

Chris's code didnt work :(

Let me explain again what IM trying to do

I have this (pseudocode):

stucture clients

name a string field

lastname a string field

age a integer field

find clients.txt and insert data from clients.txt into the clients stucture with name lastname and age

if clients.txt is not found create clients.txt and set all values as "0" in the clients structure

/* program */

/* end of program: */

collect data from strucuture clients and save all data in clients.txt

I hope that clears it up a bit :) All of this in C.

  • 0

Please someone....

I was playing around with fgets but I couldnt somehow get the function to read the file until the end of line (\o), insert into a field in the struct, then jump to the next line and insert into the next field in the struct.

  • 0

Sample C file:

#include <stdio.h>

struct client
{
  char name[15];
  char last[15];
  int age;
};

int main (void)
{
  struct client c; // a single client; make it an array for multiple clients, and remember to change the code accordingly
  FILE* client_file = fopen("clients.txt", "rt");

  /* file not found */
  if (!client_file)
	{
	  fputs("Warning: `clients.txt' was not found. Creating...", stderr);
	  client_file = fopen("clients.txt", "wt");
	  fputs("\t\t0", client_file); //assuming you are using a tab-delimited file for the fields; make sure to check the return value!!
	  fclose(client_file);
	  fputs("`clients.txt' created successfully", stderr);
	  return 1; //exit with a non-zero status (I made it positive because there wasn't an error)
	}

  /* file found */
  {
	char tmp = 0;
	for (unsigned int i = 0; (i < sizeof(c.name) - 1) || ((tmp != '\t') && (tmp != EOF)); ++i)
	  {
		tmp = fgetc(client_file);
		if ((tmp != '\t') && (tmp != EOF))
		  c.name[i] = tmp;
	  }
	c.name[sizeof(c.name) - 1] = 0;

	for (unsigned int i = 0; (i < sizeof(c.last) - 1) || ((tmp != '\t') && (tmp != EOF)); ++i)
	  {
		tmp = fgetc(client_file);
		if ((tmp != '\t') && (tmp != EOF))
		  c.last[i] = tmp;
	  }
	c.last[sizeof(c.last) - 1] = 0;

	while (((tmp = fgetc(client_file)) != '\n') && (tmp != EOF))
	  {
		c.age *= 10;
		c.age += tmp - 48;
	  }
  }

  fclose(client_file);

  return 0;
}

Sample `clients.txt' file:

Bob	Macquerel	82
George	Baeckermann	19
William	Corson		67

The sample C code would read the entry for "Bob Macquerel" and then close the file and exit, if that file existed, of course. It is untested, but it should work. Just remember to add proper error handling, and customize it to your needs.

Edit: Note that if the above sample `clients.txt' file has spaces between fields, they should be replaced by tabs. For example, "William<space><space><space>Corson<space><space>67" would become "William<tab>Corson<tab>67".

  • 0
Sample C file:

#include &lt;stdio.h&gt;

struct client
{
  char name[15];
  char last[15];
  int age;
};

int main (void)
{
  struct client c; // a single client; make it an array for multiple clients, and remember to change the code accordingly
  FILE* client_file = fopen("clients.txt", "rt");

  /* file not found */
  if (!client_file)
	{
	  fputs("Warning: `clients.txt' was not found. Creating...", stderr);
	  client_file = fopen("clients.txt", "wt");
	  fputs("\t\t0", client_file); //assuming you are using a tab-delimited file for the fields; make sure to check the return value!!
	  fclose(client_file);
	  fputs("`clients.txt' created successfully", stderr);
	  return 1; //exit with a non-zero status (I made it positive because there wasn't an error)
	}

  /* file found */
  {
	char tmp = 0;
	for (unsigned int i = 0; (i &lt; sizeof(c.name) - 1) || ((tmp != '\t') &amp;&amp; (tmp != EOF)); ++i)
	  {
		tmp = fgetc(client_file);
		if ((tmp != '\t') &amp;&amp; (tmp != EOF))
		  c.name[i] = tmp;
	  }
	c.name[sizeof(c.name) - 1] = 0;

	for (unsigned int i = 0; (i &lt; sizeof(c.last) - 1) || ((tmp != '\t') &amp;&amp; (tmp != EOF)); ++i)
	  {
		tmp = fgetc(client_file);
		if ((tmp != '\t') &amp;&amp; (tmp != EOF))
		  c.last[i] = tmp;
	  }
	c.last[sizeof(c.last) - 1] = 0;

	while (((tmp = fgetc(client_file)) != '\n') &amp;&amp; (tmp != EOF))
	  {
		c.age *= 10;
		c.age += tmp - 48;
	  }
  }

  fclose(client_file);

  return 0;
}

Sample `clients.txt' file:

Bob	Macquerel	82
George	Baeckermann	19
William	Corson		67

The sample C code would read the entry for "Bob Macquerel" and then close the file and exit, if that file existed, of course. It is untested, but it should work. Just remember to add proper error handling, and customize it to your needs.

Edit: Note that if the above sample `clients.txt' file has spaces between fields, they should be replaced by tabs. For example, "William<space><space><space>Corson<space><space>67" would become "William<tab>Corson<tab>67".

Thank you very much :) Ill test it out and post ASAP.

  • 0

Here is an example of reading a file and storing the contents in an array of structures. Then printing out the array. It should not be too difficult to adapt this to write to a file.

The expected file format is:

FirstName LastName Age
Bob Smith 32
Frank Furter 28

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MAX_CLIENTS 100

typedef struct client
{
  char nfirst[20];
  char nlast[20];
  int age;
}client;

int main(void)
{
	client c[MAX_CLIENTS];
	char line[BUFSIZ];
	char* filename = "clients.txt";
	int read_cnt = 0, l_cnt = 1;
	int i = 0;

	FILE* fp = fopen(filename, "r");

	if(!fp) {
		printf("Failed to open file: %s\n", filename);
		exit(1);
	}

	/* read the file */
	while(fgets(line, sizeof(line), fp) &amp;&amp; read_cnt &lt; MAX_CLIENTS) {
		/* parse the line and save into client array */
		if(sscanf(line, "%19s %19s %d", c[read_cnt].nfirst, c[read_cnt].nlast, &amp;c[read_cnt].age) != 3) {
			printf("--line(%d) Bad line format--\n", l_cnt);
		}else{
			read_cnt++;
		}
		l_cnt++;
	}

	fclose(fp);

	/* print the list of clients to the screen */
	for(i = 0; i &lt; read_cnt; i++) {
		printf("%s %s %d\n", c[i].nfirst, c[i].nlast, c[i].age);
	}

	return 0;
}

  • 0
Here is an example of reading a file and storing the contents in an array of structures. Then printing out the array. It should not be too difficult to adapt this to write to a file.

The expected file format is:

FirstName LastName Age
Bob Smith 32
Frank Furter 28

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MAX_CLIENTS 100

typedef struct client
{
  char nfirst[20];
  char nlast[20];
  int age;
}client;

int main(void)
{
	client c[MAX_CLIENTS];
	char line[BUFSIZ];
	char* filename = "clients.txt";
	int read_cnt = 0, l_cnt = 1;
	int i = 0;

	FILE* fp = fopen(filename, "r");

	if(!fp) {
		printf("Failed to open file: %s\n", filename);
		exit(1);
	}

	/* read the file */
	while(fgets(line, sizeof(line), fp) &amp;&amp; read_cnt &lt; MAX_CLIENTS) {
		/* parse the line and save into client array */
		if(sscanf(line, "%19s %19s %d", c[read_cnt].nfirst, c[read_cnt].nlast, &amp;c[read_cnt].age) != 3) {
			printf("--line(%d) Bad line format--\n", l_cnt);
		}else{
			read_cnt++;
		}
		l_cnt++;
	}

	fclose(fp);

	/* print the list of clients to the screen */
	for(i = 0; i &lt; read_cnt; i++) {
		printf("%s %s %d\n", c[i].nfirst, c[i].nlast, c[i].age);
	}

	return 0;
}

Great code; I understand it much better. Thank you very very much :)

My only 3 questions:

What is BUFSIZE (I know buffer size but whats it exactly mean)?

What is sscanf?

What is line?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.