Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

C / C++ Forum


You are currently viewing our C / C++ Forum as a guest. Please register to participate.
Login



Closed Thread
Help with Input, Output Files in ANSI C
Old 01-23-2011, 04:55 AM Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
the following program is syntactically correct, as far as I know.

"prog.c"
Code:
#include <stdio.h>
#define KMS_PER_MILE 1.609
 
int
main(void)
 
{
 
double miles, kms;
 
/* set pointers */
FILE *inp, *outp;
 
/* open input/output files */
inp = fopen("b:input_file.txt", "r"); 
outp = fopen("b:output_file.txt", "w");
 
/* get and echo info */
fscanf(inp, "%1f", miles);
fprintf(outp, "The distance in miles is %.2f.\n", miles);
 
/* convert distance to kms */
kms = KMS_PER_MILE * miles;
 
/* display distance in kms */
fprintf(outp, "That equals %.2f kilometers.\n", kms);
 
/* close files */
fclose(inp);
fclose(outp);
 
return(0);
 
}
____________________________________________

I've created a file called, "input_file.txt" (with a value of "112.0") which has been stored to the same file folder as "prog.c"

I have not created "output_file.txt", as that should be created automatically at the time of program execution.


I can only guess that i'm not calling "input_file.txt" correctly at this line:
Code:
/* open input/output files */
inp = fopen("b:input_file.txt", "r"); 
outp = fopen("b:output_file.txt", "w");
____________________________________________

answer written to "output_file.txt" should be 180.21
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE

Last edited by Lashtal; 01-23-2011 at 05:06 AM..
Lashtal is offline
View Public Profile
 
 
Register now for full access!
Old 01-23-2011, 12:31 PM Re: Help with Input, Output Files in ANSI C
NullPointer's Avatar
Will Code for Food

Posts: 2,883
Name: Matt
Location: Irvine, CA
Trades: 0
Just a heads up, my C is a little rusty.

What does the b: prefix before the input and output file mean? If you're trying to open the file in binary mode the b goes on the mode parameter.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
View Public Profile Visit NullPointer's homepage!
 
Old 01-23-2011, 07:36 PM Re: Help with Input, Output Files in ANSI C
King Spam Talker

Posts: 1,092
Name: Paul W
Trades: 0
Correct -- so input line should be: inp = fopen("input_file.txt", "rb"); You should always test file pointers (and indeed any pointers) for NULLs before attempting to use them - that would have shown the problem immediately. A few other points -- leave off the b for binary unless you have a reason for using it and have a look at mode "a" for append - you're always truncating the file if it exists on the second fopen.
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is offline
View Public Profile
 
Old 01-23-2011, 10:42 PM Re: Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
First of all, thank you for the advice.


Here was what I was trying to do...

- this program is to be ran only once, via batch mode. (so there will be no recursion/re-running of the program, no user input)

- it reads "input_file.txt", with a predefined value (already instantiated)

- then writes the answer to "output_file.txt"


basically, I was trying to figure out how to use Input and Output files in C

the premise is basically the same in PHP, except without pointers.


from: http://en.wikipedia.org/wiki/C_file_...le_using_fopen

"The C standard provides for two kinds of files—text files and binary filesalthough operating systems are not required to distinguish between the two."

so, does it matter whether "b:" is included, or not at the beginning of inp = fopen?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
View Public Profile
 
Old 01-23-2011, 11:15 PM Re: Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Quote:
Originally Posted by NullPointer View Post
What does the b: prefix before the input and output file mean?
That's a good question. I don't know.


it was used in the book i'm studying right now, to open, read and write to files. So I assumed it was standard C syntax.

after googling uses for "b:" however, I have found nothing.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
View Public Profile
 
Old 01-23-2011, 11:27 PM Re: Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Have also tried the following (revised code snippet) without success:

Code:
/* open input/output files */
inp = fopen("input_file.txt", "rb"); 
outp = fopen("output_file.txt", "w");

am puzzled, so think i'm going to ask around on multiple C forums as well, see what they have to say.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
View Public Profile
 
Old 01-24-2011, 07:16 AM Re: Help with Input, Output Files in ANSI C
King Spam Talker

Posts: 1,092
Name: Paul W
Trades: 0
If you write in binary you have to read binary. If you write in text you have to read text. One of the main reasons for using binary files is that it allows easier positioning of the file pointer (through writing via structures). "b:" is not C, "rb" and "wb" are.
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is offline
View Public Profile
 
Old 01-29-2011, 12:36 AM Re: Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
then, i'm going to write the authors of the book i'm using and ask them why they used "b:" in the way that they did for the example they put forth demonstrating input/output file operations.

I've been googling it, can't find it anywhere.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
View Public Profile
 
Old 01-29-2011, 05:31 AM Re: Help with Input, Output Files in ANSI C
King Spam Talker

Posts: 1,092
Name: Paul W
Trades: 0
Quote:
Originally Posted by Lashtal View Post
then, i'm going to write the authors of the book i'm using and ask them why they used &quot;b:&quot; in the way that they did for the example they put forth demonstrating input/output file operations.

I've been googling it, can't find it anywhere.
I've never heard of it, it's not part of ANSI C - are they using some sort of proprietary C precompiler? If so, naughty - it would be non-portable code. The other explanation is that they're just wrong - it does happen - see the crappy C texts by Herbert Schildt, for example.
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is offline
View Public Profile
 
Old 01-29-2011, 09:08 PM Re: Help with Input, Output Files in ANSI C
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
(hope this is okay)

some good answers: http://cboard.cprogramming.com/c-pro...ut-output.html


this stupid little thing has tripped me up for a week now! lol
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
View Public Profile
 
Old 02-04-2011, 09:41 AM Re: Help with Input, Output Files in ANSI C
wayfarer07's Avatar
Poo on You

Latest Blog Post:
jQuery Mapbox Updated
Posts: 4,040
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by Lashtal View Post
(hope this is okay)

some good answers: http://cboard.cprogramming.com/c-pro...ut-output.html


this stupid little thing has tripped me up for a week now! lol
lol.... must be an OLD textbook. I guess C hasn't changed though.
__________________
Software engineer at
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
View Public Profile Visit wayfarer07's homepage!
 
Old 02-07-2011, 06:43 PM Re: Help with Input, Output Files in ANSI C
chrishirst's Avatar
Defies a Status

Posts: 43,971
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Same answer as here http://tycoontalk.freelancer.com/c-c...ml#post1142005
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Closed Thread     « Reply to Help with Input, Output Files in ANSI C
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46181 seconds with 11 queries