writing to a text file line by line

G

grinder

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.
 
S

spibou

grinder said:
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
 
D

Default User

grinder said:
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
 
S

Simon Biber

grinder said:
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.
 
G

grinder

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.
 
S

Simon Biber

grinder said:
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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top