Question:
Reading in a file into an array c++?
carolinafootball101
2009-04-25 15:25:16 UTC
I need to read in the following contents from a file:

6
vs 100.0 1000.0
r1 2.0 20.0
r2 4.0 100000.0
r3 3.0 100000.0
r4 6.0 100000.0
r5 4.0 100000.0
r6 8.0 100000.0
rL 10.0 20.0 77.5

my file opens fine, I think I need to read the inputs in to a 2d array, but how can i do that? coz the file has different data types. should i use fscanf(i dont know how to use it too well), or should i read in each character of the file?
Four answers:
Lie Ryan
2009-04-25 15:53:42 UTC
When you read a file, what you get is always a string. It is your job convert this string into something more useful like int or float. This file format is a bit of mystery, cause I don't know what it is for, but from what I can guess:

- The first line contains the number of items in the file, in this case how many r1, r2, r3, ... is in the file. Use this to determine the array size (depending on the situation, you may either need to create an array of size exactly 6 or 6+c where c is a constant or with at least size 6).

- Then you've got a vs line. I'm not sure what this is supposed to mean.

- The next 6 lines are r1-r6. Fill these into the array you created in step 1. You can split the lines into three parts and create a 2-dimensional array.

- The last line is rL, I'm also not sure what it is but it may be r-Last. This one is a bit different from the other 'r's since it have 3 floats.
Ciaron
2009-04-25 15:57:44 UTC
Well fscanf is a C function for file handling although you can compile C programs in a C++ environment you don't need to use the C file handling in C++ I find C's file handling can be a bit confusing at times. So I am assuming you are using an input stream

what you can do is something like this

fstream in;

int count=0;

int data [6][6];// a 2 dimensional array of int

char ch [2]

in.open("test.dat") // open the file.



do

{ in.get(ch);

in>>data[i]>>data[i+1];

i++;

} while (!in.eof())

in.close();
millicent
2016-10-18 05:16:57 UTC
I observe here: an int (6), and eight rows. the 1st 7 have a 2 char string and a pair of floats, the final row has a 2 char string and 3 floats. it sort of feels to me in case you go away out ther rows which initiate vs and the rL you may have a 6 by way of two array of floats as: glide array[6][2]; for (int i=0; i<6;i++) { infile >> rowstring; for int (j=0;j<2;j++) infile >> array [i][j]; } you will desire to have the ability to paintings out the the remainder of it for your self. till all of us comprehend what different records you prefer from the report, we gained't furnish any further than that.
2009-04-25 15:57:20 UTC
You could do it multiple ways; by far the simplest to code would be to just read it all as a 2D array of strings, but it will get annoying when you try to use the data.



There's also the pair class in the standard C++ library to combine two types into one type (basically like a class), so you could use that, but it would be annoying too.



I think your best bet would be to use a void*, and just allocate the necessary memory for each element.



Alternately, you could make each column of data a vector (http://www.cplusplus.com/reference/stl/vector/ ), and then have a void* as an array of vectors, e.g. (this code wasn't tested, you'll probably have to adjust for typecasts)



vector col1;

vector col2;

vector col3;



void* cols[] = {&col1,&col2,&col3};



the thing is, you'll have to cast it whenever you use it, either to a vector or a string* or double* (if vector memory is contiguous). That could get annoying.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...