insert title string into a file

M

Magix

Hi,
I would like to replace title string into a file. My output file looks like
this:

Name Age Address
==== === ======
Ken 23 New York City
Rebecca 25 Toronto



The data list will be appended. But I only want the title string appear once
at the top only:
Name Age Address
==== === ======

I think each time I append the data, i will replace the same string title
for the 1st and 2 nd line. or you have better idea ?

FILE *fptr;
char fileName[40];

// open file.
if ((fptr = fopen("text.txt", "aw"))== NULL)
{
printf("Error: Couldn't create file.\n");
exit(1);
}

// replace the 1st and 2nd line string title each time append.

// append into file
fprintf(fptr, "%s %s %s ", name, age, address );
fprintf(fptr, "\r\n");
fclose(fptr);


Thanks.

Dimension.
 
E

Emmanuel Delahaye

Magix wrote on 27/07/04 :
I would like to replace title string into a file. My output file looks like
this:

Name Age Address
==== === ======
Ken 23 New York City
Rebecca 25 Toronto

The data list will be appended. But I only want the title string appear once
at the top only:
Name Age Address
==== === ======

I think each time I append the data, i will replace the same string title
for the 1st and 2 nd line. or you have better idea ?

I'm not sure I undestand what you mean.

Sounds to be a design question, but the 'tile' lines are only
interesting in some 'output report'. They don't need to be stored, but
it doesn't harm...

Let's see the code.
FILE *fptr;
char fileName[40];

// open file.
if ((fptr = fopen("text.txt", "aw"))== NULL)

This invokes an undefined behaviour. You must choose between "w"
(create) or "a" (append).
{
printf("Error: Couldn't create file.\n");
exit(1);
}

// replace the 1st and 2nd line string title each time append.

There is nothing like 'replace' concerning streams. It's 'write'
(create a new file) of 'append' (add to an existent file or create it
if new).
// append into file
fprintf(fptr, "%s %s %s ", name, age, address );
fprintf(fptr, "\r\n");

You want one line:

fprintf (fptr, "%s %s %s\n", name, age, address);

Design issue : note that blank separated fields can be tricky if you
have a datum with blank (an address, for example). A more solid format
might be used, like fixed field width or comma separated fields (CSV).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top