By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

How to load and output a text file in C?

P: 2
I need a code in C where accepts inputs coming from an external text file and will give an output stored on another text file. For example, a C program to compute the sum of the two integers. The value of the two integers are taken in a text file, example "input.txt" and written in this manner:

2 3

where 2 is the value of the first integer and 3 is the value of the second integer. The code should load the text file and assign variables to the values. I need to output the result in a separate text file example "output.txt" and should contain the sum of the two integers which in this example it's:

3

I guess it should be stored in an array. I just don't know what function to use to load and outpu a text file. Actually, our project is like this, the input text conatins the following info:

<p i>
<p 0> <p 0 int1> <p 0 int2>
<p 1> <p 1 int1> <p 1 int2>
.
.
.
<p n-1> <p n-1 int1> <p n-1 int2>

where <p i> is the total number of processes, <p 0> is the process 1, <p 0 int1> is the value of the first integer of <p 0> process, <p 0 int2> is the value of the first integer of <p 0> process... and so on...

example (input.txt):

3
1 3 2
2 3 4
3 4 5

the output should be in a textfile (output.txt) and would look like this:

<p i>
<p 0> <p 0 sum2>
<p 1> <p 1 sum1>
.
.
.
<p n-1> <p n-1 sumn-1>


example (output.txt):

3
1 5
2 7
3 9

Please, give me the code.... Need it badly.
Sep 23 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Look you the functions

fopen
fclose

fread
fwrite
fgets
fprintf
fgetchar
fputchar

on line or in you handy C programming book.
Sep 23 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.