Save text files in vectors

0

I have the following code

#include <stdio.h>
#include <string.h>

int main()
{
  FILE *fp1;
  FILE *fp2;
  int i,j;
  int bin[7], dec[70];
  int c;

  fp1 = fopen("bin.txt", "r");   //Abrimo  el archivo
  fp2 = fopen("dec.txt","r");

  for (i=0;i<7;i++) {                                  
    fscanf(fp1, "%d", &bin[i]);          
    printf("%d ", bin[i]);
  }

  for (j=0;j<70;j++) {                                   
    fscanf(fp2, "%d", &dec[j]);            
    printf("%d ", dec[j]);
  }

  fclose(fp1);

  return 0;
}

I need to do some operations with these vectors, clean them and then fill them again with the second line of the text file, but this only reads one line.

I would like to know how to read line by line until the end of the file.

The text file comes as follows

0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 1 0
0 0 0 0 0 1 1
0 0 0 0 1 0 0
0 0 0 0 1 0 1
0 0 0 0 1 1 0
0 0 0 0 1 1 1
0 0 0 1 0 0 0
0 0 0 1 0 0 1
0 0 0 1 0 1 0
    
asked by Sebastian Mateus Villegas 03.07.2017 в 17:29
source

3 answers

1

Response made from @eferion's response

As the questions you focus on knowing how to read each row of the file, and after each reading to make an operation with the vector obtained to reuse it in the next reading, here I pass the code that I think serves as a structure for what questions:

#include <stdio.h>

int main()
{
  FILE *fp1;
  int i;
  int bin[7];

  fp1 = fopen("bin.txt", "r");   //Abrimo  el archivo

  while (!feof(fp1)) 
  {
    for (i=0; i<7; ++i) {                                  
      fscanf(fp1, "%d ", &bin[i]);          
      printf("%d ", bin[i]);
    }

    /* AQUÍ HACES LA OPERACIÓN CON EL VECTOR OBTENIDO */
  }

  fclose(fp1);

  return 0;
}

The code assumes that the file has the structure you have indicated: rows of 7 integers.

You do not have to worry about line breaks because fscanf() ignores the blank characters (blanks, tabs, and line breaks) until you find a data of the indicated type (in this case, whole). But, if you notice, there is a space just after %d in fscanf() . This way you ensure that, once you have read the last number, ignore all the blank characters until the end of the file, avoiding returning to the loop to try to read new numbers when all that is left are line breaks or blank characters. / p>     

answered by 03.07.2017 / 19:18
source
2

Unlike the accepted answer to me I do not like the use of while(!feof(fp)) , since in this way the last item is obtained 2 times, this happens because fscanf when reading the last item does not set EOF in the stream, so that !feof(fp) returns true and re-executes what is inside while once more.

Considering the above I find that it is better to use the function fscanf within the loop while taking advantage of the fact that fscanf returns the number of assignments made, as in this case an integer is read at the same time would use something as while (fscanf(fp, "%d", &number) == 1) to read each file number one time only.

Then you could simply use a whole number to determine which index of the arrangement you would be reading, when this number has the value of 7 you will know that you read a row completely, perform your operation and then set the number to 0.

Considering the above, I wrote this code that probes and works perfectly for what you want to do.

#include <stdio.h>

int main() {
  FILE *fp;
  int i, number;
  int array[7];

  fp = fopen("bin.txt", "r");

  i = 0;

  while (fscanf(fp, "%d", &number) == 1) {
    printf("%d ", number);
    array[i] = number;

    i++;

    if (i == 7) {
      printf("Realizar operacion aqui\n");
      i = 0;
    }
  }

  fclose(fp);

  return 0;
}

When you run it you get this output:

0 0 0 0 0 0 0 Realizar operacion aqui
0 0 0 0 0 0 1 Realizar operacion aqui
0 0 0 0 0 1 0 Realizar operacion aqui
0 0 0 0 0 1 1 Realizar operacion aqui
0 0 0 0 1 0 0 Realizar operacion aqui
0 0 0 0 1 0 1 Realizar operacion aqui
0 0 0 0 1 1 0 Realizar operacion aqui
0 0 0 0 1 1 1 Realizar operacion aqui
0 0 0 1 0 0 0 Realizar operacion aqui
0 0 0 1 0 0 1 Realizar operacion aqui
0 0 0 1 0 1 0 Realizar operacion aqui

I think you do not need to mention that where I put printf("Realizar operacion aqui\n") is where your vector operation would go.

Greetings and luck!

    
answered by 12.07.2017 в 18:17
1

To read line by line you have to check if you have reached the end of the file ... which you can get with feof() :

while( 1 )
{
  for(i=0;i<7;i++){                                  
    fscanf(fp1, "%d", &bin[i]);
    if( feof(fp1) ) break; // Despues de la lectura, ojo!!!

    printf("%d ", bin[i]);
  }
}

Although, on the other hand, saying that that code is C ++ is a lot to say. Actually your algorithm is C ... but compatible with C ++. To say that the code is C ++ I would expect to find std::ifstream instead of FILE* :

std::ifstream file("bin.txt");
file.open();

if(!file.is_open())
{
  // error
}

while( true )
{
  for( int i=0; i<7; i++ )
  {
    if( !file >> bin[i] ) break;
    std::cout << bin[i] << ' ';
  }
}
    
answered by 03.07.2017 в 17:34