[color=Black][/color]hi, i need an c code for my project work, c code which states....

statement:- c program which accepts or reads 4 floating point numbers from an another file or input file say 'input.txt'(file name where 4 floating numbers where saved)and save this values in 4 char(say r0, r1, r2, r3)and perform some calculation(say addition of all 4 values) and display it on the screen and also send that value to other file(say 'output.txt')
[it should be done by using fopen and fclose operation]
pls help.


  • How is the input file layout? For example, is each number on a seperate line or are all four numbers on one line?

    you can use either fscanf directly or fgets and conversion functions to store your floats.

    do you mean convert the float to a string? if so, use sprintf
    sprintf(char_array, "%f", float_variable);

  • input file layout looks like this,
    r0 r1 r2 r3
    10 20 30 40
    20 30 40 50

    for first time it should accept values 10,20,30,40 and stores it in a,b,c,d variables.

    later some mathematical calculations is their for that i have written code.

    the problem im facing is, im not able to read 4 integer values from input file and to store it in 4 variables[its for my later use in program]
    (instead of taking floats let it be integer values)
    the contents in my input file,can either in array or as i shown above.
    Issue is it should read 4 int values and store them in different variables.
  • are r0-r3 significant or just some marker? I mean, do you need to process and store them too? If not,

    char buf[80];
    int data[4] = { 0 };
    FILE *fp = fopen("input.txt", "r");

    if(fp) {
    while(fgets(buf, sizeof buf, fp) != NULL) {
    if((sscanf(buf, "%d %d %d %d",
    &data[0], &data[1], &data[2], &data[3])) == 4) {
    // data[0-4] contain current line in file
    } else {
    // ints weren't converted and stored
    // r0-r3 will take this path

    "data" need not be an array; it can be four separate variables. if later you want the floats, change the sscanf to %f instead of %d and change the ints to floats.

    oops; didn't catch values were for later use right away. If you need to keep and store each line in the file for later use, you can do a couple of things:

    calculate and store results for each line inside of the while loop... you'd need an array or if know there's only two lines of ints, just use two variables and count the lines in the file.

    another thing you could do is use a 2D array for ints.

    int data[NUM_OF_LINES_TO_STORE][4] = { { 0 } },
    idx = 0;
    while(fgets(...)) {
    if((sscanf(buf, "%d %d %d %d",
    &data[idx][0], &data[idx][1], etc)) == 4) {
    // increment idx on successful storing

    something like that.


  • r0-r3 were just marker.
    thanks a lot.. i got it..:)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!