1

I have this function that saves in an integer variable all the numbers from a text file. But I want to make a change so that I can save just the second number in each line into a vector and then print the whole vector. Here is an example of the file.txt:

123  19
321  18
432  9
876  16
875  17

And here is the code that must be changed:

void LerVetor(int *V, int *N)
{
    FILE *fp;
    int marks;
    fp = fopen("dados3.txt", "r");

    if (fp == NULL)
        printf("Falha ao abrir ficheiro\n");

    rewind(fp);

    do
    {
        fscanf(fp, "%d", &marks);
        printf("%d\n", marks);

    } while (!feof(fp));

    fclose(fp);
}

The output is the same as the file.txt because the code just prints the content of the file.

Resume: Save just the second numbers of each line, ex: 19, 18, 9..., in a vector and then print the vector.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • regarding: `rewind(fp);` the file was just opened, so the 'file pointer' will already be at the beginning of the file, so this statement is not needed – user3629249 Mar 24 '20 at 16:17
  • OT: regarding: `printf("Falha ao abrir ficheiro\n");` Error messages should be output to `stderr`, not `stdout` and when the error indication is from a c library function, should also output (to `stderr`) the text reason the system thinks the error occurred. Suggest using: `perror( "fopen failed" ); as that will output both the error message and the text reason to `stderr`. – user3629249 Mar 24 '20 at 16:19
  • 1
    Please read: [while(!feof(fp)) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Mar 24 '20 at 16:21
  • 1
    OT: regarding: `fscanf(fp, "%d", &marks);` Always check the returned value (not the parameter values) to assure the operation was successful. Note: the `scanf()` family of functions returns the number of successful 'input format conversion' specifiers. In the current scenario any returned value other than 1 indicates an error occurred – user3629249 Mar 24 '20 at 16:24
  • So what are `int *V` and `int *N` for? – anastaciu Mar 24 '20 at 16:29
  • If you want the second number you can just use two format specifiers, i.e. scanf("%d %d", &a, &b) and ignore the result in a. – Thomas Dignan Mar 24 '20 at 16:34

2 Answers2

2

the following proposed code:

  1. cleanly compiles
  2. groups the variables with where they are used
  3. properly checks and handles I/O errors
  4. read/displays the second number from each line of the file
  5. performs the desired functionality

and now, the proposed code:

#include <stdio.h>
#include <stdlib.h>


void LerVetor( void ) 
{
    FILE *fp = fopen("dados3.txt", "r");
    if( !fp )
    {
        perror("fopen to read: dados3.txt failed");
        exit( EXIT_FAILURE );
    }

    int marks;        
    int dummy;
    while( fscanf(fp, "%d %d", &dummy, &marks) == 2 )
    {
        printf("%d\n", marks);
    }

    fclose(fp);
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
1

One way you can do it is to pass a pointer to the vector (array of ints) where you need to store the values as a parameter of the function.

To dispose the first value in every line you can use a discard specifier %*d:

Live sample

#include <stdio.h>

void LerVetor(int* vector)
{
    FILE *fp; 

    if (!(fp = fopen("dados3.txt", "r"))){
        perror("Falha ao abrir ficheiro");        //print eventual open file error
        return;
    }

    // keep reading lines discarding first value with %*d avoiding container overflow
    for(int i = 0; fscanf(fp, "%*d %d", &vector[i]) == 1 && i < 100; i++) 
        printf("%d\n", vector[i]);                //printing vector values

    fclose(fp);
}

int main(void)
{
    int vector[100];
    LerVetor(vector);
    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53