473,320 Members | 1,933 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,320 software developers and data experts.

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 6159
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
5
by: Alan Searle | last post by:
I am exporting ms-access data to XML files. This works fine. However, I need to insert one line at the top of each exported file (i.e. a reference to the XSL file) and am having a problem with...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
1
by: nasirmajor | last post by:
Dear all i have the following code for writing to the existing file. =========================== void AppendToFileEng() { SqlDataReader gr = null; string engpath2=""; gr =...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
5
by: dh87lfc | last post by:
Hi, I have almost got my script to do what I want it to do, but I have one final problem for which I have been searching for a solution. My code now successfully reads the content of every text file...
0
by: Steve Holden | last post by:
Mohsen Akbari wrote: Then, later:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.