0

We need to make an integer array from a file such a way that the upper line of file would be size and the next line of numbers would be in an array having size of upper line integer value.

We read integers from the file. I am trying to solve by using a counter in a while loop that increments if we move to next line. odd number lines(1,3,5,7..) have only one integer which is size for an array and even number lines(2,4,6,8...) will have numbers intended to be in an array.

Now, when counter is odd(when 1) its looking on first line in file. This will determins the size of an array which numbers are in the 2nd line.

Example if a file has

 3        //(line1) count=1(odd) -this case-> array size found is 3 
 5 6 2    //(line2) count=2(even) -this case-> array of size 3 is made
 5        //(line4) count=3(odd) -this case-> array size found is 5
 3 5 7 1 6 //(line4) count=4(even) -this case-> array of size 5 is made


3 will be used as a key to allocate the array of size 3 for [5 6 2]
then,
5 will be used as a key to allocate the array of size 3 for [3 5 7 1 6]
int main()
{
    int count=1;
    FILE* file = fopen("file.txt", "r");

    int num ,i, element;
    while (!feof (file) && num > 0){
        if (count%2==0){
            fscanf (file, "%d", &num);
        }
        else{
            int *arr = (int *)malloc(num*(sizeof(int)));

            fscanf (file, "%1d", &element);

            arr[i] = element;






            free(arr);

        }
        count++;
    }
    fclose (file);

    return 0;
}

I don't know how should i use the (count) to find the number of line we are iterating in a file.

  • 1
    Ask yourself what the value of `num` is when *first* going into that while-condition. Hint: your program doesn't know either. That aside, your post doesn't have a question. – WhozCraig Oct 25 '19 at 07:56
  • So what's the problem? – klutt Oct 25 '19 at 07:59
  • 3
    Please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Oct 25 '19 at 08:05
  • 1
    You should [edit] your question and tell us what is going wrong, e.g. the expected and actual output. We cannot test your program because `print_arr()` is missing. Showing the intended purpose of the variables by either using meaningful names or by adding comments would be helpful. Example: What is counted by `count`? Why is the initial value `2`? – Bodo Oct 25 '19 at 08:24
  • 1
    Some general advice: With your `fscanf` calls you don't know which line is currently being parsed, so you may get out of sync if you have wrong input, i.e. if the 2nd line does not contain the expected number of values or if it contains an invalid character. I suggest to read the lines using `fgets`, process 1st and 2nd line differently and check for errors. You could use a loop with `strtol` to parse the numbers in the 2nd line. The `endptr` output value can be used for error checking. It will tell you the position where the conversion of the number stopped. You should check this character. – Bodo Oct 25 '19 at 08:38
  • A good compiler should warn you about uninitialized variables (`i`, `num`) – Mathieu Oct 25 '19 at 08:48
  • `fscanf` doesn't usually care about the difference between end-of-line and other whitespace, so you could forget about counting lines. Just read a number, allocate the array and use a `for` loop to read the numbers for the array. Then do whatever processing you need to do with the data values. Rinse and repeat until you fail to read a number. – Ian Abbott Oct 25 '19 at 09:03
  • `fscanf` isn't very good at dealing with well-defined *lines* of input. (`fscanf` is more stream-oriented.) If you want to solve this problem without using `fscanf`, see [What can I use for input conversion instead of `scanf`?](https://stackoverflow.com/questions/58403537/what-can-i-use-to-parse-input-instead-of-scanf) – Steve Summit Oct 25 '19 at 11:35

0 Answers0