468,484 Members | 2,075 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

writing to a text file line by line

first off, i am an extreme newbie to C. i am an undergrad research
assistant and i have been shifted to a project that involves building a
fairly involved c program. The part that i am stuck on now is as
follows:

- i am trying to write code that will take any number of text inputs
(names of other text files) and put them into a file line-by-line at
the users request. meaning, i want the user to be able to type the
strings and enter them subsequently into a file. this file will then be
read line-by-line by another program which will intern use the lines as
references to data files.

so, i am trying to figure out a way to prompt the user to enter the
text(s) one after another and have them store in a file.

i have been building the bigger program for a while now and really want
to move off of the software and on to the hardware, and i cant do taht
until i finish this program in its entirety.
i have tried various method to write to a file line-by-line but cant
get it to work the way i have described above.
if anyone can help me i will owe you the world.

Aug 2 '06 #1
5 5642
grinder wrote:
first off, i am an extreme newbie to C. i am an undergrad research
assistant and i have been shifted to a project that involves building a
fairly involved c program. The part that i am stuck on now is as
follows:

- i am trying to write code that will take any number of text inputs
(names of other text files) and put them into a file line-by-line at
the users request. meaning, i want the user to be able to type the
strings and enter them subsequently into a file. this file will then be
read line-by-line by another program which will intern use the lines as
references to data files.

so, i am trying to figure out a way to prompt the user to enter the
text(s) one after another and have them store in a file.

i have been building the bigger program for a while now and really want
to move off of the software and on to the hardware, and i cant do taht
until i finish this program in its entirety.
i have tried various method to write to a file line-by-line but cant
get it to work the way i have described above.
I don't understand your problem very well. Ideally
you should give some examples like what the user
might enter at the keyboard and what should be written
in the file.

In the meantime here's an example : assume that the
string line contains one line which should be written.
Then you do fprintf(file,"%s\n",line) ;
Or is your problem how to form line to begin with ?

Spiros Bousbouras

Aug 2 '06 #2
grinder wrote:
first off, i am an extreme newbie to C. i am an undergrad research
assistant and i have been shifted to a project that involves building
a fairly involved c program. The part that i am stuck on now is as
follows:

- i am trying to write code that will take any number of text inputs
(names of other text files) and put them into a file line-by-line at
the users request. meaning, i want the user to be able to type the
strings and enter them subsequently into a file. this file will then
be read line-by-line by another program which will intern use the
lines as references to data files.

so, i am trying to figure out a way to prompt the user to enter the
text(s) one after another and have them store in a file.

i have been building the bigger program for a while now and really
want to move off of the software and on to the hardware, and i cant
do taht until i finish this program in its entirety.
i have tried various method to write to a file line-by-line but cant
get it to work the way i have described above.

We're not going to write code for you. You'd have been better off
showing us what you've done so far, and explaining EXACTLY what doesn't
work about it.

At any rate, things like fopen(), fgets(), fputs(), etc, might figure
into the solution.


Brian
Aug 2 '06 #3
grinder wrote:
first off, i am an extreme newbie to C. i am an undergrad research
assistant and i have been shifted to a project that involves building a
fairly involved c program. The part that i am stuck on now is as
follows:
- i am trying to write code that will take any number of text inputs
(names of other text files) and put them into a file line-by-line at
the users request. meaning, i want the user to be able to type the
strings and enter them subsequently into a file. this file will then be
read line-by-line by another program which will intern use the lines as
references to data files.
Your description is very unclear. If the input lines are names of other
text files, are we supposed to open those text files and insert the
contents of them into the output file? Or just output exactly what the
user wrote?
so, i am trying to figure out a way to prompt the user to enter the
text(s) one after another and have them store in a file.
Here is a way to prompt the user to enter lines of text, and store what
the user wrote into a file.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
/* declare an int variable to hold characters */
int ch;

/* open a file for output: */
FILE *fp = fopen("output.txt", "w");

/* check that it opened: */
if(fp == NULL)
{
fprintf(stderr, "Error opening output file\n");
exit(EXIT_FAILURE);
}

/* give instructions to user */
puts(
"Enter your lines of text and they will be written\n"
"into the output.txt file. When you are finished,\n"
"generate an end-of-file condition. On Unix this is\n"
"done by pressing Ctrl-D whereas on Windows/DOS it is\n"
"done by pressing F6 or Ctrl-Z then Enter.\n");

/* read characters from stdin and put them into the file */
while((ch = getchar()) != EOF)
{
if(putc(ch, fp) == EOF)
{
fprintf(stderr, "Error writing to output file\n");
exit(EXIT_FAILURE);
}
}

/* close the file */
if(fclose(fp) == EOF)
{
fprintf(stderr, "Error closing output file\n");
exit(EXIT_FAILURE);
}

return 0;
}

It doesn't actually consider lines of text as such, instead just reading
and writing character by character. Due to the line buffering in most C
implementations you can edit each line as you type it, and it will only
be submitted to the program and written to the file after you press the
Enter key.

--
Simon.
Aug 3 '06 #4
thank you simon. i was not expecting for anyone to write my code. i
tried to explain my situation well enough that someone could understand
my predicament. thank you for the example , i just needed to see
something similar to what i wanted so i could get a good visualization
of the procedure. i will use your example as a template of sorts.

Aug 3 '06 #5
grinder wrote:
thank you simon. i was not expecting for anyone to write my code. i
tried to explain my situation well enough that someone could understand
my predicament. thank you for the example , i just needed to see
something similar to what i wanted so i could get a good visualization
of the procedure. i will use your example as a template of sorts.
You're welcome. When reading my code, notice how careful I was to check
the result of each function I called. Make sure you are careful about
checking the result of each function.

C makes it easy to ignore errors and produce code that works most of the
time, but when it fails you have no idea why. At least with my code you
get an error message, and you can narrow it down to a particular location.

--
Simon.
Aug 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by melanieab | last post: by
reply views Thread by Yunus's Group | last post: by
3 posts views Thread by bbepristis | last post: by
5 posts views Thread by Alan Searle | last post: by
1 post views Thread by nasirmajor | last post: by
19 posts views Thread by rmr531 | last post: by
5 posts views Thread by dh87lfc | last post: by
reply views Thread by Steve Holden | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.