| tempester86 (4) | |||||||
|
Hello. I have a problem. I have a huge txt file - 10.4 MB (13316 lines by 75 columns) that I have to read into an array. Values are separated by spaces, end of line is '\n', not space. I have tried multiple code variations with no success. Now I just got to this function.
At one point in the process I added a verifier at the end of the function to see if it did it's job.
It did not write out anything. Noting that I wrote separate functions for readfromfile, sizeofline, linesinfile, columns [last two in case the program needed to be used for files with different numbers of lines/columns). I am just stuck on this one. I thought it could be because of the size of the file. It does take abou 10 seconds just to calculate the number of lines in the file. But when I try to use the read in matrix function, the program just goes nuts, or enters a weird loop that makes no sense. I made alot of test functions to verify each step of the way. I will also attach the full code here.
I hope someone can help me. Thank you. | |||||||
|
|
|||||||
| infoartenovo (15) | |
|
What I don't understand is: why do you read such a big file in a 4 by 5 array of char:)? It seems a case of buffer overflow: After you overcome the size of your array you are writing on the stack outside the limits of your program. Probably, you wanted to read the matrix variable and, by mistake, you used the A variable. Hope it'll help. Ciao | |
|
Last edited on
|
|
| tempester86 (4) | |
| and what is the limit of an array? | |
|
|
|
| infoartenovo (15) | |
|
Anyway, the problem is not the limit of an array in C, but the fact that you are using the wrong array in the wrong place. Your A array is a 4x5 array, but you try to access and write in memory locations that are outside your array (in the main you call Readm(13316,75)). Moreover, even if you substitute the A array with the matrix array in the Readm, you have to check its use in the function. In fact, being matrix a 100x15000 array you can't call the Readm with (13316,75). But you can safely call Readm with (75,13316). Hope it helps. Ciao. | |
|
Last edited on
|
|
| tempester86 (4) | |
|
Thank you. I fixed the problem with the array reading. I got the code so far that I let a small problem slide. Thanks for your help. :) Now all I have to do is include a function that will put every value in the string array into a double array, so i can work with the actual numbers in the text file. Does C++ have a special function for that or do I have to figure out a way to convert string to double. I am not sure about the process as I know string is 8 bit I think and double, well, it shouldn't be just 8. Any ideas would be great. thanks again, guys! | |
|
|
|
| Darkmaster (341) | |
|
http://www.cplusplus.com/reference/cstdlib/atof/ http://www.cplusplus.com/reference/string/string/c_str/ | |
|
|
|
| tempester86 (4) | |||
|
Hello. I need some more help on the program. I get this weird error and it aborts. It's because of the stod function i am using to convert the string array into a double array. If I use matrix[0][0] = stod(A[0][0]); it works, but it aborts if i try to use the for sequence. I am attaching the code.
| |||
|
|
|||
| MikeyBoy (175) | |
I'd recommend using a debugger, so that you can see what values R, C and A[R][C] have at the point where your program crashes. That might give you a clue as to why it crashes.Edit: I also recommend using some named constants for your array sizes, rather than magic number. That will help keep things consistent when you're making changes, and if you name them sensibly, it will give other people some understanding of what the numbers are supposed to represent and why they are set to those values. | |
|
Last edited on
|
|