473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Write a structure onto a file

Hello guys
I've never dealt with files in C language.
I would like to know if the code I wrote is correct. It seems to work but I'm not sure about the syntax.

<code>
FILE *f1;
struct service {
char city[40];
char train_station[40];
};

struct service train_stop;

strcpy(train_stop.city, "1");

entree = fopen("list_cities", "a");

while(strcmp(train_stop.city,"ZZ") != 0)
{
printf("Enter a city (ZZ to end): ");
scanf("%s", train_stop.city);

if(strcmp(train_stop.city,"ZZ") != 0)
{
fprintf(entree, "%s\n", train_stop.city);

printf("Enter the corresponding train station: ");
scanf("%s", train_stop.train_station);
fprintf(entree, "%s\n", train_stop.train_station);
}
}
fclose(f1);
}
</code>
Apr 12 '12 #1
1 1276
weaknessforcats
9,208 Expert Mod 8TB
Files don't know about structs.

I suggest your write each struct member individually and in the case of the arrays write 40 bytes each. That way you can read 40 bytes each back into a struct variable.

If you write 40 bytes plus 40 bytes plus a \n, you will have what is called a flat file. In flat files, each record has the same layout and this makes for easier processing when the file read.

You need to absolutely know the order of the data in the file in order to read that file.
Apr 12 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
7
by: Bob | last post by:
Hi, I am trying to use BULK INSERT with format file. All of our data has few bytes of header in the data file which I would like to skip before doing BULK INSERT. Is it possible to write...
2
by: jeff_zhang446 | last post by:
Hi, I try to write some data structure into a file as below and would appreciate if someone can give me some advice here. The output file looks like below: row(1) 100 1 0 0 0 0 0 0 0 ...
20
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer? --------------------------------------------------- typedef unsigned char UCHAR; int...
2
by: TJ | last post by:
Dear Sir/Madam I have made one user control to embed in Internet Explorer I just wrote one method to keep track log something like. public static void WriteLog(..) StreamWriter stWriter =...
3
by: broli85 | last post by:
I am writing an extension for Firefox 1.5. I would like to know if there is a way to write and read data to and from a file which is in the location of the extension (or somewhere in the user's...
2
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
5
by: sangith | last post by:
Hi, I have a file which is in the following format: This file is classify.txt #######Alphabets######### . . . #######Numbers##########
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
1
kirubagari
by: kirubagari | last post by:
Im having issue whereby i have to filter out the data and stored in abnormal log file.my code as below and i have attach the file to give the clearer picture of the scenario.I need to filter the data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.