In the following code, I have a function that is called in main by the line - 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! -
#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
-
}
-
your error is on line number 15
in the function Size is defined as pointer. and you are trying multiply pointer with integer. -
//long *Array = malloc(Size * sizeof(long)); // Error code
-
long *Array = malloc((*Size) * sizeof(long)); // correct here
-
to understand call by reference follow this link 3 6887
your error is on line number 15
in the function Size is defined as pointer. and you are trying multiply pointer with integer. -
//long *Array = malloc(Size * sizeof(long)); // Error code
-
long *Array = malloc((*Size) * sizeof(long)); // correct here
-
to understand call by reference follow this link
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. -
-
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));
-
}
-
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). -
-
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);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |