Home > Software design >  Reading a file and going ahead only when the number of values in two lines are same
Reading a file and going ahead only when the number of values in two lines are same

Time:06-22

I'm trying a tackle a problem but I am not understanding how I can go about it. The task is as follows:

  1. There is a txt file like this(1st number being the number of values to be worked with):
5 1.1 2.2 3.3 4.4 5.5
5 5.5 4.4 3.3 2.2 1.1
2 0.1 -0.1
2 1 1
5 1 2 3 4 5
6 1 2 3 4 5 6

And the output should be as follows:

6.6 6.6 6.6 6.6 6.6
1.1 0.9
Error(Number n1 != Number n2)

I am supposed to come up with a function read(file *fp, array[]) for reading the file, add(a1[], a2[], sum[]) to add numbers from two separate lines and output(sum[]) to give the result on the screen and call them in main(). The task is to add the values to their respective positions from the following line and if the number of elements aren't same, give an error.

I am thinking along the lines that I should read the first line as long as I don't reach '\n' and once reached, go to the next line and so on. But I am not able to write a function to read from the file. This is my attempt but I don't know how to move ahead.

float read(FILE *fp, float number[])    //For reading individual rows from txt file
{
    int count;
    if(fscanf(fp, "%d", &number) != 1) return -1;
    
    for(int i = 0; i != '\n';   i) {
        if(fscanf(fp, "%f", &number[i]) != 1) {
            return -1; 
        }
    }
    
    return number[0];
}

I'd be really grateful if someone could help with this. Thank you in advance.

CodePudding user response:

if someone could help with this.

Algorithm

  1. Read an integer --> n1.

  2. Allocate memory for n1 numbers: float sum[n1]

  3. In a loop, read rest of line into sum[].

  4. Read an integer --> n2.

  5. If n1 != n2, print error, else in a loop (n1 times), read an FP, sum with previous sum[i] and print. Free memory either way.


Note: This approach does not well handle mal-formed input. More advanced code needed to detect non-numeric input, scant/extra numbers per line, missing 2nd line, out-of-range number count, out of memory, the usual suspects ....

CodePudding user response:

You don't need to detect the newline because each line has the count of items. So just read that many items.

Yuo should read the first number into count, then use that as the limit of the loop. The return type of the function should then be int.

int read(FILE *fp, float number[])    //For reading individual rows from txt file
{
    int count;
    if(fscanf(fp, "%d", &count) != 1) return -1;
    
    for(int i = 0; i < count;   i) {
        if(fscanf(fp, "%f", &number[i]) != 1) {
            return -1; 
        }
    }
    
    return count;
}

CodePudding user response:

How about an outside-the-box thinking approach?

Instead of forming an array to store the floating point data, use 2 file pointers to read the data.

Perhaps not quite what OP was looking for, yet it does avoid float memory management issues.

// Demonstrative code

// Return -1 on error,
// return 0 on number mis-match,
// else return 1
static int read_line_pair(FILE *f1, FILE *f2) {
  int n1, n2;

  fscanf(f2, "%*[^\n]");  // Skip line
  if (fscanf(f1, "           
  •  Tags:  
  • c
  • Related