| | |
How to copy floating point values from text file to array
![]() |
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdio.h> #include<io.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<string.h> void minmax(int); float max[13],min[13],array[13][270]; void main() { FILE *fp; char line[100]; char symptom[10]; float f; fp=fopen("input.txt","r"); int i=0,j=0,k,l=0; int rw_cnt = 0; int cl_cnt = 0; int len; while(!feof(fp)) { //i=0; //l=0; fgets(line,99,fp); printf("%s \n",line); // getch(); rw_cnt++; // len=strlen(line)+; // line[len]='\0'; //for(i=0;line[i]!='\n';i++) for (i=0;i<50;i++) { while(line[i]!=' ')//&& line[i]=='\n') { symptom[j]=line[i]; j++; i++; cl_cnt++; } symptom[j]='\0'; printf("Symptom= %s \n",symptom); printf("Symptom = %f \n",atof(symptom)); /*for(int l = 0; l<10;l++) { symptom[l]=0; }*/ j = 0; } printf(""); } cout << rw_cnt; /* f=atof(symptom); array[l][k]=f; l++; i++; j=0; k++; printf("array=%d",array[l][k]); } */
I want to separate each column separately in the array and find min and max of each array.
Thanks in advance
Last edited by Ancient Dragon; Feb 21st, 2008 at 7:52 am. Reason: corrected code tags
line 1: delete it because stream.h is not a valid header file except in some very old compilers such as Turbo C++. Since you are writing a C program you can't use it here. Also remove most of the other header files because they aren't needed. Looks like all you need is stdio.h and stdlib.h.
line 12: int main() not void main(). main always returns an integer.
line 11. If the file contains 270 rows and 13 columns of data then the arrays you declared are backwards. Also not how to initialize them to 0 values.
After using fgets() to retrieve a line you can use strtok() to split it up into individual parts
line 12: int main() not void main(). main always returns an integer.
line 11. If the file contains 270 rows and 13 columns of data then the arrays you declared are backwards. Also not how to initialize them to 0 values.
C Syntax (Toggle Plain Text)
float array[270][13] = {0.0F}; float max[270] = {0.0F}; float min[270] = {0.0F};
After using fgets() to retrieve a line you can use strtok() to split it up into individual parts
C Syntax (Toggle Plain Text)
int column = 0; char *token = strtok(line," "); while( token != NULL) { array[row][column] = atof(token); token = strtok(line); }
Last edited by Ancient Dragon; Feb 21st, 2008 at 8:08 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
Thanks for the help. I tried the suggestion but it didnt help.Then I tried code which is given below. I have used iostream because i am going to use c++ also.
But now my problem is, code works only for the fixed number of rows and columns.
But I want to make it work for any no of row and column .
please help me
c Syntax (Toggle Plain Text)
#include "stdafx.h" #include<stdio.h> #include<math.h> float example[14][270]; int main() { //printf("Hello World!\n"); FILE *fp; const int nLineSize = 4096; char line[nLineSize + 2]; char symptom[10]; int i=0,j=0,k=0,m=0; int row_cnt=0; float val; fp=fopen("inputk.txt","r"); if (fp == NULL) { perror ("Error opening file"); exit(1); } for(k=0;k<270;k++)// loop for no. of the rows { i=0; m=0; line[0] = 0; fgets(line,nLineSize,fp); row_cnt++; puts (line); while(line[i]!='\n'&&m<13) { while(line[i]!=' ') { symptom[j]=line[i]; j++; i++; } val=atof(symptom); example[m][k]=val; m++; i++; j=0; } } symptom[0]=line[i]; val=atof(symptom); //cout<<val; example[m][k]=val; cout<<"total rows:="<<row_cnt<<endl; for( i=0;i<270;i++) { for(j=0;j<13;j++) cout<<example[j][i]<<" ";//display the array cout<<"\n"; } }
But now my problem is, code works only for the fixed number of rows and columns.
But I want to make it work for any no of row and column .
please help me
Last edited by Ancient Dragon; Mar 8th, 2008 at 9:49 am. Reason: Corrected code tags -- use [code], not <code>
use const int values to define the array dimensions then use those variables in the code. Or you could do something like this:
C Syntax (Toggle Plain Text)
int rows = sizeof( example ) / sizeof( example[0] ); int cols = sizeof( example[0] ) / sizeof(float); for(k=0;k< rows ;k++) // line 24 { }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C Forum
- Previous Thread: Using fgets() properly
- Next Thread: Pointers and strings
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






