Computing.Net > Forums > Programming > write to file function

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

write to file function

Reply to Message Icon

Original Message
Name: zerolink
Date: May 18, 2003 at 19:26:41 Pacific
Subject: write to file function
OS: Windows XP home
CPU/Ram: 1.4 ghz/1 gig ram
Comment:

hello. i was wondering how would i write a function that would allow me to save an array of stucts (for example an inventory system) to a file. i want to be able to have the user type in a name that it saves to. i also was wondering how i then can have the user input that file back in so they would have their inventory back.
thank you for the help.

zerolink


Report Offensive Message For Removal


Response Number 1
Name: MrNiceGuy (by vng2k)
Date: May 19, 2003 at 19:42:37 Pacific
Reply: (edit)

You mean in what language?
Here is basicly what you have to do:
open a file with a filehandle
write to the file using the filehandle
close the file.

Goodluck


Report Offensive Follow Up For Removal

Response Number 2
Name: zerolink
Date: May 20, 2003 at 05:34:11 Pacific
Reply: (edit)

I got this far on saving it to a file:
void savetofile(Node *parts)
{
char filenamea[10],filenameb[20]={"./"};

printf("Enter name of file to create: ");
scanf("%s",filenamea);

strcat(filenameb,filenamea);
strcat(filenameb,".dat");
filenameb[strlen(filenameb)]='\0';

FILE * thefile=fopen(filenameb,"wt");

fprintf(thefile,Node &parts);

fclose(thefile);
}

However the trouble im having is in the fprintf line. i want to pass it the struct of information but not sure how. i have parts pointing to the struct.

and i am using visual C++

thanks again


Report Offensive Follow Up For Removal

Response Number 3
Name: MrNiceGuy (by vng2k)
Date: May 21, 2003 at 01:32:08 Pacific
Reply: (edit)

What I see in the code is you are trying to write the file in Acci. YOu need to think about a binary file instead. fprintf() is the same as printf() only diff is one write to the file and other write to IO. Try to use Write() instead. I have a very short example.

struct exam
{
int a;
char b[20];
};

The total memory allocated for the struct will be 24 bytes begin at integer a (assume that you are using PC). Got it?

Goodluck


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software