By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Loading file into an array line by line

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

Expand|Select|Wrap|Line Numbers
  1.  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!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "sorting.h"
  4.  
  5. long *Load(char *File, int *Size) {
  6.  
  7.   FILE *fp = fopen(File, "r");
  8.   if (fp == NULL) { // check to see if file opens; if NULL, failed
  9.     printf("Error opening file. \n");
  10.     return 0;
  11.   }
  12.  
  13.   fscanf(fp, "%d", Size); // First line of input file is designated as the total number of elements in file; store it into "Size"
  14.  
  15.   long *Array = malloc(Size * sizeof(long)); // Error is here
  16.   if (Array == NULL) {
  17.     printf("Out of memory!\n");
  18.     return 0;
  19.   }
  20.  
  21.   while (! feof(fp)) {
  22.     fscanf(fp, "%ld", Array);  // Store all numbers into Array
  23.   }
  24.  
  25.   fclose(fp);
  26.   return(Array); // Return array for main.c
  27. }
  28.  
Sep 26 '10 #1

✓ answered by johny10151981

your error is on line number 15

in the function Size is defined as pointer. and you are trying multiply pointer with integer.
Expand|Select|Wrap|Line Numbers
  1. //long *Array = malloc(Size * sizeof(long)); // Error code
  2. long *Array = malloc((*Size) * sizeof(long)); // correct here
  3.  
to understand call by reference follow this link

Share this Question
Share on Google+
3 Replies


100+
P: 1,059
your error is on line number 15

in the function Size is defined as pointer. and you are trying multiply pointer with integer.
Expand|Select|Wrap|Line Numbers
  1. //long *Array = malloc(Size * sizeof(long)); // Error code
  2. long *Array = malloc((*Size) * sizeof(long)); // correct here
  3.  
to understand call by reference follow this link
Sep 26 '10 #2

P: 6
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. long *Load(char *File, int *Size) {
  3.   //long *Array = NULL;
  4.  
  5.   FILE *fp = fopen(Filename, "r");
  6.   if (fp == NULL) { // check to see if file opens; if NULL, failed
  7.     printf("Error opening file. \n");
  8.     return 0;
  9.   }
  10.  
  11.   fscanf(fp, "%d", Size); // Set Size equal to first line of the file
  12.  
  13.   long *Array = malloc((*Size) * sizeof(long));
  14.   if (Array == NULL) {
  15.     printf("Out of memory!\n");
  16.     return 0;
  17.   }
  18.  
  19.   while (! feof(fp)) {
  20.     fscanf(fp, "%ld", Array);
  21.     count++;
  22.   }
  23.  
  24.   fclose(fp);
  25.   return(Array);
  26. }
  27.  
  28. int Save(char *File, long *Array, int Size) {
  29.   FILE *fp = fopen(File, "w");
  30.   int count;
  31.   if (fp == NULL) { // check to see if file opens; if NULL, failed
  32.     printf("Error opening file. \n");
  33.     return 0;
  34.   }
  35.  
  36.   for (count = 0; count < &Size; count++) {
  37.     fprintf(fp, "%ld", Array[count]);
  38.   }
  39.   fclose(fp);
  40.   return (sizeof(Array));
  41. }
  42.  
Sep 26 '10 #3

P: 6
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).

Expand|Select|Wrap|Line Numbers
  1.  
  2. long *Load(char *File, int *Size) {
  3.  
  4.   int count = 0;
  5.  
  6.   FILE *fp = fopen(File, "r");
  7.   if (fp == NULL) { // check to see if file opens; if NULL, failed
  8.     printf("Error opening file. \n");
  9.     return 0;
  10.   }
  11.  
  12.   fscanf(fp, "%d", Size); // Set Size equal to first line of the file
  13.  
  14.   long *Array = malloc((*Size) * sizeof(long));
  15.   if (Array == NULL) {
  16.     printf("Out of memory!\n");
  17.     return 0;
  18.   }
  19.  
  20.   while (count < *Size) {
  21.     Array[count] = fgetc(fp);
  22.     count++;
  23.   }
  24.  
  25.   fclose(fp);
  26.   return(Array);
  27. }
  28.  
  29. int Save(char *File, long *Array, int Size) {
  30.   FILE *fp = fopen(File, "w");
  31.   int c;
  32.   if (fp == NULL) { // check to see if file opens; if NULL, failed
  33.     printf("Error opening file. \n");
  34.     return 0;
  35.   }
  36.   fseek(fp, 0, SEEK_END);
  37.  
  38.   c = getc(fp);
  39.   while (c != EOF) {
  40.     fputc(c, fp);
  41.     c = fgetc(fp);
  42.     Size++;
  43.   }
  44.   fclose(fp);
  45.   return (Size);
  46. }
  47.  
Sep 26 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.