How to load and output a text file in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • galivio
    New Member
    • Sep 2006
    • 2

    How to load and output a text file in C?

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Look you the functions

    fopen
    fclose

    fread
    fwrite
    fgets
    fprintf
    fgetchar
    fputchar

    on line or in you handy C programming book.

    Comment

    Working...