473,405 Members | 2,421 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,405 software developers and data experts.

a function that store data from file to an array

2
hi guys, I have question about writing a function that will store some data from a file in an array, then when that function is call in main, I can use that array in the main() for calculation. Here is what I have for that function:

void input(double Array[], int n)
{
int t;
//double * Array;
cout << "enter dimension of Array" << endl;
cin >> n;
Array = new double [n];
ifstream inputData ("c:\\test.txt");
if(inputData.is_open())
{
//inputData.open( "c:\test.txt" );
for(t=0;t<n;t++)
{
inputData >> Array[t];
cout << Array[t] << endl;
}
inputData.close();
}else
{
cout << "unable to open file.";
}
}

now if I put what isnt the void into the main code it will work fine, I am not sure how can I return this function so that when I call out this function in main, it will take the data in test.txt and store in the array I assigned in main.

when I call out this function in main like this:
void input(&Array, n);

complier will have a error saying 'input' illegal use of type void and too many initializers.

I am not sure how to correctly declare this function or call this function, would be nice if someone can help me clear things up =)

thanks~
Feb 13 '08 #1
3 4203
Laharl
849 Expert 512MB
Well...first off, do your file I/O in input() if you're going to use it. To pass in an array that needs to be changed for other use, either a) use a vector& since you're working in C++, or b) pass a pointer to the array, eg int*[] or int** due to array decay. In the Howtos section, weaknessforcats has written a very good tutorial on arrays in C/C++ that you should go read before he c/ps it in this thread.
Feb 13 '08 #2
almo
2
thanks Laharl, I gone through the examples and stuff and clean up the code a little bit... so right now I have the file I/O in a separate function and it gets call in the the main function.

I am declaring the input function like this:

void input(double *Array[], int n)
{
int t;
*Array = new double [n];
ifstream inputData ("c:\\test.txt");
if(inputData.is_open())
{
for(t=0;t<n;t++)
{
inputData >> *Array[t];
cout << Array[t] << endl;
}
inputData.close();
}else
{
cout << "unable to open file.";
}
}

but when I call it in main like this:

void input(&Data, n);

the compiler will return error C2182: 'input' : illegal use of type 'void'
and C2078: too many initializers

I am not sure what I am doing wrong in here, should I use double or something else instead of void when I declare "input" ? I tried using double instead of void and return 0 at the end of the function and that C2182 error will be gone, but I still have the too many initializer error

any hint?
Feb 13 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
void input(&Data, n);
You are calling the function. The void only appears in the function prototype and function definition.

You should:
input(&Data, n);
In the Howtos section, weaknessforcats has written a very good tutorial on arrays in C/C++ that you should go read before he c/ps it in this thread.
Actually, I have not written an article so I will need to c/ps it in this thread anyway. Here it is:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Feb 13 '08 #4

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
11
by: Colin Steadman | last post by:
Hope this makes sense! I'm building an ASP page which allows uses to add items to an invoice via a form, ie: Item No Part No Order No Quanity Units Price VAT ------- ...
3
by: T | last post by:
Hi all. I have a problem I have not been able to find a reference about. I am using VB6 and am only a hobbyist programmer. I have 7 arrays of type MyData. Type MyData has 23 elements. Which...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
9
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common...
1
by: jef.d | last post by:
I am attempting to read through a text file & then update an HTML page table w/ the output from the text file (ie; statusing by table). What I want the code to do is read through the file, look...
6
by: tuxy94 | last post by:
I do not understand why the variable @data_file keeps the data from the previous call. My goal is to use this in order to clean a file from specific lines I do not desire, I do so, but if I run...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.