Loading file into an array line by line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryokubaka
    New Member
    • Sep 2010
    • 6

    #1

    Loading file into an array line by line

    In the following code, I have a function that is called in main by the line

    Code:
     Array = Load_File (Filename, &Size);
    The function is suppose to do, as the title would suggest, load a file of numbers (one number per line, of N lines) into an array. I had it running at one point but got a segfault, and realize I needed to allocate memory for my array. However, as the code below is, I get an error on line 20 that says "error: invalid operands to binary * (have 'int *' and 'unsigned int'). I understand the error, but don't know how to fix it. I think I am not finding Size correctly.

    I haven't coded in quite a while, so I would appreciate some help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "sorting.h"
    
    long *Load(char *File, int *Size) {
    
      FILE *fp = fopen(File, "r");
      if (fp == NULL) { // check to see if file opens; if NULL, failed
        printf("Error opening file. \n");
        return 0;
      }
    
      fscanf(fp, "%d", Size); // First line of input file is designated as the total number of elements in file; store it into "Size"
    
      long *Array = malloc(Size * sizeof(long)); // Error is here
      if (Array == NULL) {
        printf("Out of memory!\n");
        return 0;
      }
    
      while (! feof(fp)) {
        fscanf(fp, "%ld", Array);  // Store all numbers into Array
      }
    
      fclose(fp);
      return(Array); // Return array for main.c
    }
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    your error is on line number 15

    in the function Size is defined as pointer. and you are trying multiply pointer with integer.
    Code:
    //long *Array = malloc(Size * sizeof(long)); // Error code
    long *Array = malloc((*Size) * sizeof(long)); // correct here
    to understand call by reference follow this link

    Comment

    • ryokubaka
      New Member
      • Sep 2010
      • 6

      #3
      Thank you very much! That fixed the problem and has this function working properly.

      The next function I am working on is meant to take the array that was just stored and output it entirely into a new file. I have the base for it (coded below), but my main function is saying that only 4 elements stored in the array are being copied, yet when I check the output file it only displays the last element from the array, and the rest are 0's. No errors, just functionally incorrect..

      I actually think the error is not from the Save function, but still in the Load function. I'm unsure of how I can use the fscanf function to continue scanning into an array -- I think it's only scanning one by one.

      Code:
      long *Load(char *File, int *Size) {
        //long *Array = NULL;
      
        FILE *fp = fopen(Filename, "r");
        if (fp == NULL) { // check to see if file opens; if NULL, failed
          printf("Error opening file. \n");
          return 0;
        }
      
        fscanf(fp, "%d", Size); // Set Size equal to first line of the file
      
        long *Array = malloc((*Size) * sizeof(long));
        if (Array == NULL) {
          printf("Out of memory!\n");
          return 0;
        }
      
        while (! feof(fp)) {
          fscanf(fp, "%ld", Array);
          count++;
        }
      
        fclose(fp);
        return(Array);
      }
      
      int Save(char *File, long *Array, int Size) {
        FILE *fp = fopen(File, "w");
        int count;
        if (fp == NULL) { // check to see if file opens; if NULL, failed
          printf("Error opening file. \n");
          return 0;
        }
      
        for (count = 0; count < &Size; count++) {
          fprintf(fp, "%ld", Array[count]);
        }
        fclose(fp);
        return (sizeof(Array));
      }

      Comment

      • ryokubaka
        New Member
        • Sep 2010
        • 6

        #4
        The following is where I am at now. I believe it should be loading my array correctly, but saving the array to a separate file is not working at all (when I check output file, it is empty).

        Code:
        long *Load(char *File, int *Size) {
        
          int count = 0;
        
          FILE *fp = fopen(File, "r");
          if (fp == NULL) { // check to see if file opens; if NULL, failed
            printf("Error opening file. \n");
            return 0;
          }
        
          fscanf(fp, "%d", Size); // Set Size equal to first line of the file
        
          long *Array = malloc((*Size) * sizeof(long));
          if (Array == NULL) {
            printf("Out of memory!\n");
            return 0;
          }
        
          while (count < *Size) {
            Array[count] = fgetc(fp);
            count++;
          }
        
          fclose(fp);
          return(Array);
        }
        
        int Save(char *File, long *Array, int Size) {
          FILE *fp = fopen(File, "w");
          int c;
          if (fp == NULL) { // check to see if file opens; if NULL, failed
            printf("Error opening file. \n");
            return 0;
          }
          fseek(fp, 0, SEEK_END);
        
          c = getc(fp);
          while (c != EOF) {
            fputc(c, fp);
            c = fgetc(fp);
            Size++;
          }
          fclose(fp);
          return (Size);
        }

        Comment

        Working...