简体   繁体   中英

how to Read large input - 8MBdata in C

I have a problem where i have to read a large data (8mb) from the input file.I tried giving the size of array . Is there any effective way i can rewrite the code

#include <stdio.h>
#include <stdlib.h>
int main()
 {

FILE *f;
char msg[9000000]=" ";  
int i=0;
f=fopen("file.txt","r");
while((msg[i]=fgetc(f))!=EOF){
        i++;
        }
 printf("\nThe data from the file is :%s\n",msg);
fclose(f);
return 0;
 }

In this case, you can just write what you read without saving all of that on memory.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{

    FILE *f;
    int msg;
    int inputExists = 0;
    f=fopen("file.txt","r");
    if(f == NULL){
        perror("fopen");
        return 1;
    }
    printf("\nThe data from the file is :");
    while((msg=fgetc(f))!=EOF){
        putchar(msg);
        inputExists = 1;
    }
    if(!inputExists) putchar(' ');
    printf("\n");
    fclose(f);
    return 0;
}

Non-static local variables are (typically, though not required by the C standard) stored on a stack. In most systems the size of that stack is fairly limited, often around 1 MB or even less.

Thus you should store the data else where, either on the heap or in static memory. Using the heap is the preferred way:

#include <stdio.h>
#include <stdlib.h>
#define MAX (8 * 1024 * 1024)
int main () {
  char * data = malloc(MAX);
  // add error checks
  int in;
  size_t position = 0;
  while ((in = fgetc(stdin)) != EOF && position < MAX) {
    data[position++] = in & 0xFF;
  }
  // Note: data is NOT a string, no null termination
  // also check for errors
  free(data);
  return 0;
}

Two things to note: I'm also checking that the buffer won't overflow. This is very important, you should make that sure for every buffer you read from and write to.

Second, as any reference will tell you, the return value of fgetc is int . This is important because EOF can be a value that isn't representable with a char .

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