• 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.
  • Posts

    • Threads scales past half a billion users, brings deeper community and feed controls by Fiza Ali Meta has announced Threads crossing a major milestone of 500 million monthly active users. And, at the heart of this growth sits something simple: communities. From books to basketball, parenting to music, Threads says its rise has been powered by people clustering around shared interests and, in turn, giving the platform its identity. In response, the platform is expanding its Communities feature beyond beta and introducing a set of new tools designed to make participation easier and more engaging. A redesigned Communities Hub will now appear in the main navigation menu, allowing users to jump between groups without leaving their feed. Each community will also receive a distinct Community Icon, giving them clearer visual identity and making them easier to recognise across the platform. Then there’s Community Progress, which is a kind of live gauge showing how close a topic is to becoming a full-fledged community, alongside guidance on how users can contribute to its development. In addition, Meta is also expanding its Community Champions programme, recognising more users who actively contribute to community engagement. And then things go more local; Local Communities is already available in 100 countries, including North America, South America, Asia, and Europe but are now rolling out with native-language tags starting in Japan, South Korea, and Taiwan. The platform is also expanding Live Chats to more communities in the coming weeks, adding features such as co-hosting and the ability to quote moments directly into users’ feeds. Beyond communities, Meta is tightening the loop between users and their feeds. Earlier this year came "Dear Algo," a feature that lets people tell Threads what they want more or less of. Now it’s being paired with a new tool, "Your Algo." It allows people to adjust how frequently certain topics appear, with options lasting one, three, or seven days. Meta says these preferences remain private and can be managed alongside “Dear Algo” in a unified settings hub. The rollout begins in the US, Canada, UK, Australia, and New Zealand. Finally, the company says these changes are part of an ongoing effort to refine Threads based on user feedback and that further updates will continue as the platform evolves.
    • You pay just $100 per TB with this rare 4TB PCIe Gen4 NVMe SSD deal by Sayan Sen SSDs and GPUs are incredibly hard to get nowadays due to high pricing. Discounts are quite rare which is why we report on them as soon as we spot a good deal. For example AMD's new 9070 GRE was finally up for sale at a very good price of just $500 thanks to a special coupon. Sadly that deal is gone but if you happen to be looking for a 4TB NVMe SSD and can spend around $400 there is a really good offer on sale that you should not miss out on as TeamGroup's 4TB G50 model is on sale for that that price which means you are only paying $100 per TB, a very good deal in the current market (purchase link under the specs table down below). The TeamGroup T-FORCE G50 NVMe SSD is a PCIe Gen4 drive and as such it promises to deliver sequential read speeds of up to 5,000 MB/s, helping accelerate game loading, file transfers, and everyday computing tasks. Since this is a 4TB drive you can use it for a gaming library to take advantage of things like DirectStorage. The SSD features an InnoGrit controller and SLC caching technology to support consistent performance. An ultra-thin, patented graphene heatsink is included to aid in heat dissipation. Get it at the link below: Team Group T-FORCE G50 4TB Internal SSD (TM8FFE004T0C129): $449.99 + $50 off w/ promo code SSF69668, limited offer => $39.99 (Sold and Shipped by Newegg US) Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • I agree. I also think Phil stayed too long. They should definitely fire whoever thought all a console platform needed was Call of Duty, Elder Scrolls, and Fallout to survive. Asha and crew are still saying they need more Elder Scrolls and Fallout games. They simply don't get it.
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      522
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!