How do you save input in a text file?

L

Les Coover

The following is an example of input:

1 Les

L 01/02/04 2300 06:00AM SW Nashville
R 01/06/04 2400 10:00PM SW Kansas City

L 01/15/04 2440 10:00AM NW Minneapolis
R 01/15/04 3440 05:00PM NW Kansas City


2 Jen

L 01/02/04 2300 0600AM SW Nashville
R 01/06/04 2400 10:00PM SW Kansas City


Each employee #, Name can have 0 to many associated flights.

How can I build a C program to use scanf to collect the data
as input and output it to a text file? I need to be able to
display prompts using printf but I don't want the prompts
to appear in my text file.

Any help greatly appreciated.

Les
 
M

Mark A. Odell

How can I build a C program to use scanf to collect the data
as input and output it to a text file? I need to be able to
display prompts using printf but I don't want the prompts
to appear in my text file.

Look up fprintf(), then fprintf() to stdout those prompts and fprintf() to
your text file the responses once scanned in.
 
L

Les Coover

Mark

Does this look right? What do I declare fid as?

/* jeto.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
fid = fopen('jeto.txt','w');
char store[100];

while (scanf("%99s", store)== 1)
{
printf("Enter");
fprint(fid, '%99s');
}
fclose(fid)
return 0;
}
 
T

Thomas Matthews

Les said:
Mark

Does this look right? What do I declare fid as?

/* jeto.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
fid = fopen('jeto.txt','w');
char store[100];

while (scanf("%99s", store)== 1)
{
printf("Enter");
fprint(fid, '%99s');
}
fclose(fid)
return 0;
}
1. Don't top-post, replies are appended to the bottom
like this one or interspersed.
2. Look up streams in your text book or reference
manual, especially "FILE".
3. See the C FAQ below.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
D

Dan Pop

In said:
Does this look right?
Nope.

What do I declare fid as?

What does your C book say?
/* jeto.c */

#include <stdio.h>
#include <string.h>

What for?
int main(void)
{
fid = fopen('jeto.txt','w');
char store[100];

while (scanf("%99s", store)== 1)
{
printf("Enter");
fprint(fid, '%99s');
}
fclose(fid)
return 0;
}

Before worrying about file I/O, you should *really* understand the
difference between string literals and character constants. It's one
of the most basic things of the language and nothing is going to work
until you have a clear idea which is which and where you have to use one
or the other.

Dan
 
L

Les Coover

Thomas Matthews said:
Les said:
Mark

Does this look right? What do I declare fid as?

/* jeto.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
fid = fopen('jeto.txt','w');
char store[100];

while (scanf("%99s", store)== 1)
{
printf("Enter");
fprint(fid, '%99s');
}
fclose(fid)
return 0;
}
1. Don't top-post, replies are appended to the bottom
like this one or interspersed.
2. Look up streams in your text book or reference
manual, especially "FILE".
3. See the C FAQ below.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

I have read your references, other references and the text book and still
cannot get this.
Could you give me another clue.
Following code still has a problem

/* jeto.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
FILE *file; /* FILE pointer */
int em_no;
char store[100];
/* create a file for writing */
file = fopen ("jeto.txt", "w");
while(scanf ("%d %99s", &em_no, store)== 2)
{
printf("Enter, Example: 01 Les L 01/02/04 ",
"2300 06:00AM SW Nashville R 01/06/04 ",
"2400 10:00PM SW Kansas City");

fprint(file, "%d %50s\n", &em_no, store);
}
fclose(file); /* now close the file */

return 0;
}

Les
 
I

Irrwahn Grausewitz

Les Coover said:
Could you give me another clue.
Following code still has a problem

/* jeto.c */

#include <stdio.h>
#include <string.h>
You don't use anything declared in string.h.
int main(void)
{
FILE *file; /* FILE pointer */
int em_no;
char store[100];
/* create a file for writing */
file = fopen ("jeto.txt", "w");
Check 'file' here to make sure fopen succeeded.
while(scanf ("%d %99s", &em_no, store)== 2)
{
printf("Enter, Example: 01 Les L 01/02/04 ",
"2300 06:00AM SW Nashville R 01/06/04 ",
"2400 10:00PM SW Kansas City");
Wrong number of arguments. Look up printf in your C library
reference.
fprint(file, "%d %50s\n", &em_no, store);
There is no function fprint. Look up fprintf in your C library
reference. The "%d" format conversion specifier for the printf
function family expects an integer argument, not a pointer to an
integer.
}
fclose(file); /* now close the file */

return 0;
}

HTH
Regards
 
T

Thomas Matthews

Les said:
I have read your references, other references and the text book and still
cannot get this.
Could you give me another clue.
Following code still has a problem

/* jeto.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
FILE *file; /* FILE pointer */
int em_no;
char store[100];
/* create a file for writing */
file = fopen ("jeto.txt", "w");
while(scanf ("%d %99s", &em_no, store)== 2)
The function scanf() reads input from the console stream.
The function scanf() is evil:
http://www.eskimo.com/~scs/c-faq/q12.20.html
{
printf("Enter, Example: 01 Les L 01/02/04 ",
"2300 06:00AM SW Nashville R 01/06/04 ",
"2400 10:00PM SW Kansas City");
Character literals can be concatenated by the compiler:
printf("Enter, Example: 01 Les L 01/02/04 "
"2300 06:00AM SW Nashville R 01/06/04 "
"2400 10:00PM SW Kansas City");
Note the elimination of the commas (',').
Also, you should use '\n' for line breaks. The printf()
function does not automatically insert them.

fprint(file, "%d %50s\n", &em_no, store);
The first parameter should not be a pointer / address.
For %d format, fprintf() requires a value.

}
fclose(file); /* now close the file */

return 0;
}

Les

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE * out;
out = fopen("junk.txt", "w");
if(!out)
{
fprintf(stderr, "Error opening file.\n");
return EXIT_FAILURE;
}
fprintf(out, "Text written to a file.\n");
fclose(out);
return EXIT_SUCCESS;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Micah Cowan

Les Coover said:
I have read your references, other references and the text book and still
cannot get this.
Could you give me another clue.
Following code still has a problem

/* jeto.c */

#include <stdio.h>
#include <string.h>

You still don't need said:
int main(void)
{
FILE *file; /* FILE pointer */

This is obvious enough that there is no need to add a comment
declaring this fact.
int em_no;
char store[100];
/* create a file for writing */
file = fopen ("jeto.txt", "w");
while(scanf ("%d %99s", &em_no, store)== 2)
{
printf("Enter, Example: 01 Les L 01/02/04 ",
"2300 06:00AM SW Nashville R 01/06/04 ",
"2400 10:00PM SW Kansas City");

You are not using printf() properly. This is pretty much
guaranteed to cause problems. I'd suggest a correction, but the
bottom line is I'm not completely sure what exactly you want it
to do. However, for whatever it is you're trying to do, I *am*
pretty sure that you only want to pass one single argument to
printf(): There should be only one pair of double-quotes, and no
commas, unless you want them *inside* the strings (or possibly,
you might use fgets).

Also, you're scanning for input *before* you are prompting for
it. Once you get the order of operations sorted out, you'll
probably discover you'll need to fflush(stdout) to be safe.
 

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

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top