![]() |
|
#1
|
|||
|
|||
Filling 2D array with .txt file valuesI'm trying to write a simple program that will take values from a .txt file that are seperated by tabs and put them into a 2D array. I'm using gcc to compile. I first need to find the dimensions (rows and columns) of the data in .txt file. Below is what I came up with - it almost works. Here's the problem I'm encountering: When I call finddimension_y() first I get ydim=11 (which is correct), and xdim=0 (obviously not correct). If I call finddimension_x() first I get xdim=501 (correct), and ydim=10(not correct). It seams to me that the library function getc() starting at the beginning of the file after the first function is called, it just continues getting characters from wherever it left off. If that's the problem any ideas how to fix it? The .txt file is attached and the code is below. Thanks.
CPP / C++ / C Code:
~clueless |
|||
|
#2
|
|||
|
|||
Re: Filling 2D array with .txt file valuesQuote:
See, for example: getc() Reference Quote:
Either: 1. Close the file after returning from one function and open it again before calling the other. or, a little more elegantly: 2. Use rewind() to set the stream pointer back to the beginning of the file. See, for example: rewind() Reference You are going to have to do this again before reading file contents into your matrix, right? Regards, Dave |
|
#3
|
|||
|
|||
Re: Filling 2D array with .txt file valuesGreat thanks!! It works perfectly...but you already knew that...ha ha.
Anyway, I'm not sure what you mean by your question. I haven't figured out how to populate the array now that I know its dimensions. ~I'm clueless |
|
#4
|
|||
|
|||
Re: Filling 2D array with .txt file valuesThanks again for your help. I found that resource site very helpful.
I figured out how to fill in the array and it worked pretty well, so of course I decided to complicate things. I wnated to dynamically allocate the space for the 2D array. I read one of your previous posts on how to do that, and I think that part works, but something else happened along the way. When I execute the code below with the line 'image[R][C-1] = extractpixelvalue();' commented out, the values for R (row) and C (column) are currect, when I uncomment that line the values for R and C are no longer correct, and the matrix that prints in the output window is only partly correct. Between my last version and this one I changed (among other things) the extractpixelvalue function. I was using a combination of fseek and fgetc, in this version I'm using fscanf. Are fgetc (from findtab()) and fseek (from extractpixelvalue()) conflicting with eachother? Thanks for any and all help. CPP / C++ / C Code:
~I'm Clueless |
|
#6
|
|||
|
|||
Re: Filling 2D array with .txt file valuesI noticed a couple of things that may or may not be a problem for you. What happens when your text file has only one entry? Let's say it has a number with no tab or newline character. When you count the dimensions, you'll get zero for both of them. Secondly, your code becomes difficult to read through when you use pointers like that. You make it look like you have an array when you simply have a single value. To dereference a single int from an int*, use the * operator in front of it. Like:
CPP / C++ / C Code:
Somebody please correct me if I'm wrong, but I believe that you can simply call fscanf() over and over again without removing characters manually. See this example: CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Filling 2D array with .txt file valuesThank you fakepoo.
I'm still struggling on the dynamically allocated array. I've been using this as a reference: Quote:
I'm now trying to create a function called alloc that I can call multiple times from multiple .c files to allocate multiple arrays of different names and sizes. I really have no idea where to even start. Below is my function (taken from the example above), but I can't figure out how to pass in a different name for **a. CPP / C++ / C Code:
On top of that, I need to be able to fill these arrays from different functions, so I think I need to use pointers to these arrays after they have been allocated. But the name of the array is already a "pointer to a pointer of an int" Can I point to a pointer to a pointer of an int? Is any of this even possible? If it sounds like I have no idea what I'm doing, it's because I don't. Any help would be greatly appreciated. ~I'm Clueless |
|
#8
|
|||
|
|||
Re: Filling 2D array with .txt file valuesTry returning the pointer to the array. Then, if you want to do something like fill the array, pass the pointer into the function that fills it.
CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: Filling 2D array with .txt file valuesOk, sorry I can't figure this out. fakepoo, when I tried your exemple, I got an error that said "syntax error before '*' token". I'm not really sure what that code was suposed to do either. sorry.
Here's what I have now and it seems to be working, but I want to pass the name of an array into the function so I can allocate space for multiple arrays. It would also be handy if the arrays were global so I could read/write from different functions in different .c files. CPP / C++ / C Code:
Thanks, ~I'm Clueless |
|
#10
|
|||
|
|||
Re: Filling 2D array with .txt file valuesHere is how you would use the code that I posted before:
CPP / C++ / C Code:
Does it make sense now? |
Recent GIDBlog
Review: Gel laptop cooling pad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Power Calibration Error In Nero Fix (hopefully) | matt3678 | Computer Hardware Forum | 60 | 20-Aug-2009 06:04 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 21:03 |
| CD Buring Failed | skanth2000 | Computer Hardware Forum | 1 | 15-Nov-2003 04:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The