473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loading file into an array line by line

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

3 6887
johny10151981
1,059 1GB
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
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
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

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

Similar topics

1
by: google | last post by:
Hello, I'm having major problems trying to get my head round this problem. I'm trying to generate an error free script, however, I still cannot sort out this loop. I get a "Notice:...
5
by: Michael Kennedy [UB] | last post by:
Hi, I would like to report some weird behavior which results in an internal compiler error in VS.NET 2003 (VC++). I have an ATL COM dll project which also uses MFC. This solution (workspace...
9
by: Marco Nova | last post by:
Hello I'm using the latest version of Visual Studio 2003 version 7.1.3088, .net framework 1.1.4322 and I've some problem compiling a project, it give me the error ...
0
by: lawrence | last post by:
Hi, I made a MFC ActiveX with a CwinThread Class in VC ++. Then i used it on Visual basic. When i try to make an exe i got this error : Debug Assertion Failed! Program: C:\PROGRAM...
0
by: CjB | last post by:
Good Morning, I am trying to get PHP+Oracle to work on Solaris 8. I have installed Apache with the following: ./configure --enable-module=so When I start apache it works fine. I think build...
1
by: pukya78 | last post by:
Hi, I am trying to get the current file and the line number which is getting executed. I used: MessageBox.Show(New StackTrace(New StackFrame(True)).GetFrame(0).GetFileLineNumber) which gives me...
3
by: edfialk | last post by:
Hi, all I'm trying to do is load the php_mapscript.so library into php. Here's the php file: Code: dl("php_mapscriot.so") if (extension_loaded("MapScript")) echo "works!"; else echo...
1
by: vaskarbasak | last post by:
Hi All, I have a text file.The file size is very big.I have to read the file from different line number. e.g File test.txt contains 10000000 lines and more.The 1st thread read the file from...
1
by: Imran78 | last post by:
How I can nsert filename in text file at each line e.g I have file name abc.dat My data 1 My data 2 My data 3 I want to convert it with abc.dat My data 1, abc.dat
1
by: ywang123 | last post by:
I have a test.csv file with multi line in one field like following: Name, message "Gus", "See you" "Amy", "take to you later, Thank you. call me" "Mark", "Try it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.