943,937 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2792
  • C++ RSS
Apr 19th, 2007
0

Reading in data into array (special case, please see)

Expand Post »
Hi there,
I need to make a piece of code in C that
1. opens a specified file,
2. Reads the data in the file and separates it as shown below,
3. Converts the strings into integers and then perfoms some manipulation on them,
4. returns the new values to a second text file (possibly, not quite sure on this one yet).
My data is given in the following format (there is one space between a1 and b1, b1 and c1, etc.):
x
y
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
while not EOF
I would like to read in data in the following format for further processing:
x
y
a[i] b[i] c[i] d[i] e[i] f[i]
a[i+1] b[i+1] c[i+1] d[i+1] e[i+1] f[i+1]
I guess I need to read it in as an array and then do some sort of concatenation/speration (my inputs can be one or two digits, i.e. a[i] can be 0 or 99). This is probably not very difficult, but I haven't touched C in about 6-7 years. It must be done in C (no C++ or C#). I tried to search, but couldn't find my case (maybe it was posted before and I just did not see it).

Please give me some hints. Timewise, I should be able to have it working ASAP.

Thank You very much.
Last edited by Rob111; Apr 19th, 2007 at 11:47 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rob111 is offline Offline
14 posts
since Apr 2007
Apr 20th, 2007
0

Re: Reading in data into array (special case, please see)

not clear what a1 b1 c1 ... represents.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 20th, 2007
0

Re: Reading in data into array (special case, please see)

not clear what a1 b1 c1 ... represents.
a1 b1 c1 are integers, which can vary between 0 and 99.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rob111 is offline Offline
14 posts
since Apr 2007
Apr 20th, 2007
0

Re: Reading in data into array (special case, please see)

first, create the arrays of appropriate size. If you know the number of rows and columns in the file it will make it a lot easier. But if you don't (and normally you will not) then you will just have to dynamically allocate them with malloc().

Easiest way is to put it into one large array
  1. // array to be allocated
  2. int *array = 0;
  3. int x = 0;
  4. int y = 0;
  5.  
  6. // open the file for reading
  7. FILE* fp = fopen("file","r");
  8. // read x and y
  9. fscanf(fp,"%d", &x);
  10. fscanf(fp,"%d", &y);
  11.  
  12. // allocate the array
  13. array = (int *)malloc(y * x * sizeof(int));
  14.  
  15. // read the data into the arra
  16. int i = 0;
  17. int maxi = x * y;
  18. for(i = 0; i < maxi && fscanf(fp,"%d", array[i]) > 0; i++)
  19. ;
  20. fclose(fp);


// now you can calculate the location of any x,y cell by this simple formula
C++ Syntax (Toggle Plain Text)
  1. int row = 5; // desired row
  2. int col = 3; // desired column
  3. int cell = (<desired row number>-1) * <columns per row> ) + (<desired column number>)
  4. or
  5. cell = ((row-1) * x) + col;
Last edited by Ancient Dragon; Apr 20th, 2007 at 10:09 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 20th, 2007
0

Re: Reading in data into array (special case, please see)

Thanks Ancient Dragon,

I probably was not very clear on the task, so here are clarifications:
- x & y have no impact on the # of rows and columns
-I do not know the number of rows in the file and the number of rows will vary from file to file
-there are always 6 elements in a row (or line), but number of digits in each element can be 1 or 2 (i.e. a1 can vary from 0 to 99); the elements (columns) are separated by one space
-I would like to sort data into array as speficied in original post, I believe it will make the analysis better: I can just increment I to perform manipulations with the next row of data.

Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rob111 is offline Offline
14 posts
since Apr 2007
Apr 20th, 2007
0

Re: Reading in data into array (special case, please see)

so use realloc() to expand the 2d array to the desired size. Something like this untested code
  1. // allocate first row
  2. int **array = malloc( sizeof(int*) );
  3. // allocate 6 columns
  4. array[0] = malloc( 6 * sizeof(int));
  5. int numRows = 0;
  6. int n;
  7. int row = 0;
  8. int col = 0;
  9. while( fread(fp,"%d", &n) > 0)
  10. {
  11. if( col < 6)
  12. {
  13. array[row][col] = n;
  14. ++col;
  15. }
  16. else
  17. {
  18. ++row;
  19. array = realloc(array,row * sizeof(int*));
  20. array[row] = malloc( 6 * sizeof(int));
  21. col = 0;
  22. array[row][col] = n;
  23. ++col;
  24. }
  25. }
Last edited by Ancient Dragon; Apr 20th, 2007 at 11:52 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 21st, 2007
0

Re: Reading in data into array (special case, please see)

Getting the following error:

Error c:\lcc\projects\reading\reading.c: 23 type error in argument 2 to `fread'; found 'pointer to char' expected 'unsigned int'
Error c:\lcc\projects\reading\reading.c: 23 type error in argument 3 to `fread'; found 'pointer to int' expected 'unsigned int'
Error c:\lcc\projects\reading\reading.c: 23 insufficient number of arguments to `fread'

for the following line: while( fread(fp,"%d", &n) > 0)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rob111 is offline Offline
14 posts
since Apr 2007
Apr 22nd, 2007
0

Re: Reading in data into array (special case, please see)

I got it, you meant fscanf innstead of fread. It works. Thank you very much, Ancient Dragon.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rob111 is offline Offline
14 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bubble sort
Next Thread in C++ Forum Timeline: mulitplication of two huge integers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC