![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 3
Rep Power: 0
![]() |
I want to create a function read data of type int from a text file and stores them in a 2-dimensional array. But the number of the data in the text file is unknown during the program execution. Therefore the size of the array is also unknown.
I have problems in reading the correct data value of type int and stores them in the array. I also can't determine the correct total of columns and rows for the array. My data in the text file looks something like this: 1111111111111111111 1000101010101010101 1010101010101011111 1111111111111111111 1011110000111111111 1111111111111111111 My coding is as below: Code: #define ROWMAX 100 #define COLMAX 100 int data[ROWMAX][COLMAX]; int ROW=0,COL=0; void readdata(FILE *file,int data[ROWMAX][COLMAX]) { int c; int row=0,column=0,i,j; int temp[ROWMAX][COLMAX]; while(!feof(file)) { fscanf(file,"%d",&c); if (c!='\n' ) { temp[i][j]=c; //printf("%d",temp[i][j]); column=column+1; } else { //printf("\n"); column=0; row++; }//end if } //end while ROW=row+1; COL=column-1; printf("\nSize of array = [%d] x [%d]\n",ROW,COL); //display data for(i=0;i<ROW;i++) { for(j=0;j<COL;j++) { data[i][j]=temp[i][j]; printf("%1.4f",data[i][j]); } printf("\n"); } Does anyone knows the easiest solution to this? Please give me any source code examples for reading data of type int in text file during run time and determining the total of rows and columns for the arrays? Later, I need to scan each row of data for 0s and stores them in text file.then I access the text file to normalize all 0s by rows. Therefore I need to save the information about the location of the 0s(which rows and columns).After the normalization,I need to replace the 0s in the later text file with the new value at the correct location. How to save the location value(rows and pixels) and corresponds them with their new normalized value? Thanks in advance. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 3
![]() |
If the size is unknown, it is best to use vectors. Vectors are resizeable.
So you can simply create vector of type int, and store file contents there. #include <vector>
using std::vector;
.
.
.
vector <int> data;
for (int i=0; i<sizeof (pointerToText); i++)
{
data[i] = something;
} |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Do not double post and use [ CODE ] tags and repost your message.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|