简体   繁体   中英

Reading the contents of a file into a buffer in C

I'm working on a program which reads every integer in csv file and copies it into a buffer so that I can later use it to construct a binary search tree with it. I'll show my code, then I'll explain the issue I'm having:

Code -

int *createBuffer(int count) {
  FILE *file = fopen(FILE1, "r");
  int buffer[count + 1];
  int *bufferPointer = buffer;
  int number;
  int ch;
  int i = 0;
  while (1) {
      ch = fgetc(file);
      if(ch == EOF){
          break;
      }
    if (fscanf(file, "%i", &number)) {
      buffer[i] = number;
      i++;
    } 
  }
  return bufferPointer;
}

Count refers to the number of commas that are present in the file so I can allocate enough space for each number in the array. The file pointer points to the file I'm opening in read-only mode. The buffer is created using the aforementioned count variable. bufferPointer is the pointer to the buffer that I'm returning from the function. The while loop runs until the variable ch is equal to EOF at which point it breaks. The if statement's purpose is basically to scan the file for integers and read them into number, and then copy number into the next buffer index. Finally, the buffer pointer is returned.

This code is giving me extremely strange results. When I print the buffer, I get the result:

9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 850045856 0 -2141008008 32767 0 0 214814639 1 0 0 -2141007448 32767 0 0 214814639 1 -487430544 32766 539243238 32767 -2141007448 32767 6 0 -487430496 32766 539279361 32767 0 0 0 0 0 0 0 0 -487430272 32766 539271526 32767 92 68 68 0 0 0 69 0 -2141007976 32767 0 0 42 68 55 46 10 40 44 100 75 63 19 13 10 95 43 47 47 49 59 40 0 0 -2141006600 % 

The reason this is strange is because although I am getting some garbage values, the entire sequence from 42...40 matches numbers in my data file. I'm not exactly sure where I'm going wrong in this code so if anyone knows, please do share.

As always, if you take the time to answer or attempt to answer this question, thank you for your time. If you need further clarification, don't hesitate to ask.

This is a "fixed" version of your code. But you will notice that it does not print the first character. Lets say, if the first number in your file is, 220, then it will print 20.
The reason is - your program first takes away a character from file in c=fgetc(file) . So at first iteration, it takes away the first character 2 from 220 and then stores 20 in the memory. Thought this problem does not occur for the rest of the iterations because the first character is comma in those cases.
To go around that problem, we can just put c=getc(file) at the end of the loop. This way, after entering the loop, it reads the first number, gets rid of the comma, reads next number, gets rid of the comma....

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

int *createBuffer(int count) {
  FILE *file = fopen("filename.txt", "r");
  int* buffer = (int*)malloc(sizeof(int)*(count + 1));
  int number;
  int ch;
  int i = 0;
  while (1) {
      if (fscanf(file, "%i", &number)) {
      buffer[i] = number;
      i++;
    } 
    ch = fgetc(file);
      if(ch == EOF){
      break;
    }
  }
  
  return buffer;
}

void main(){
  int* arr = createBuffer(10);
  for(int i=0; i<10; i++){
    printf("%d ",arr[i]);
  }
  printf("\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM