How to copy floating point values from text file to array

Reply

Join Date: Feb 2008
Posts: 3
Reputation: shaka2 is an unknown quantity at this point 
Solved Threads: 0
shaka2 shaka2 is offline Offline
Newbie Poster

How to copy floating point values from text file to array

 
0
  #1
Feb 21st, 2008
  1. #include<iostream.h>
  2. #include<stdio.h>
  3. #include<io.h>
  4. #include<conio.h>
  5. #include<math.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8.  
  9. void minmax(int);
  10.  
  11. float max[13],min[13],array[13][270];
  12. void main()
  13. {
  14. FILE *fp;
  15. char line[100];
  16. char symptom[10];
  17. float f;
  18. fp=fopen("input.txt","r");
  19. int i=0,j=0,k,l=0;
  20. int rw_cnt = 0;
  21. int cl_cnt = 0;
  22. int len;
  23.  
  24. while(!feof(fp))
  25.  
  26. {
  27. //i=0;
  28. //l=0;
  29. fgets(line,99,fp);
  30. printf("%s \n",line);
  31. // getch();
  32. rw_cnt++;
  33. // len=strlen(line)+;
  34. // line[len]='\0';
  35. //for(i=0;line[i]!='\n';i++)
  36. for (i=0;i<50;i++)
  37. {
  38. while(line[i]!=' ')//&& line[i]=='\n')
  39. {
  40. symptom[j]=line[i];
  41. j++;
  42. i++;
  43. cl_cnt++;
  44. }
  45. symptom[j]='\0';
  46. printf("Symptom= %s \n",symptom);
  47. printf("Symptom = %f \n",atof(symptom));
  48.  
  49. /*for(int l = 0; l<10;l++)
  50. {
  51. symptom[l]=0;
  52. }*/
  53.  
  54. j = 0;
  55.  
  56. }
  57. printf("");
  58.  
  59. }
  60. cout << rw_cnt;
  61.  
  62. /*
  63. f=atof(symptom);
  64. array[l][k]=f;
  65. l++;
  66. i++;
  67. j=0;
  68. k++;
  69. printf("array=%d",array[l][k]);
  70. }
  71. */
I have text file of 270 rows and 13 columns consisting of floating point values.
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: How to copy floating point values from text file to array

 
0
  #2
Feb 21st, 2008
Is this a C or C++ program? You have a mixture of the two header files. It looks like mostly C so I'm moving it into the C board.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: How to copy floating point values from text file to array

 
0
  #3
Feb 21st, 2008
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.

  1. float array[270][13] = {0.0F};
  2. float max[270] = {0.0F};
  3. float min[270] = {0.0F};

After using fgets() to retrieve a line you can use strtok() to split it up into individual parts
  1. int column = 0;
  2. char *token = strtok(line," ");
  3. while( token != NULL)
  4. {
  5. array[row][column] = atof(token);
  6. token = strtok(line);
  7. }
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: shaka2 is an unknown quantity at this point 
Solved Threads: 0
shaka2 shaka2 is offline Offline
Newbie Poster

Re: How to copy floating point values from text file to array

 
0
  #4
Mar 8th, 2008
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.
  1. #include "stdafx.h"
  2. #include<stdio.h>
  3. #include<math.h>
  4.  
  5. float example[14][270];
  6. int main()
  7. {
  8. //printf("Hello World!\n");
  9.  
  10. FILE *fp;
  11. const int nLineSize = 4096;
  12. char line[nLineSize + 2];
  13. char symptom[10];
  14. int i=0,j=0,k=0,m=0;
  15. int row_cnt=0;
  16. float val;
  17. fp=fopen("inputk.txt","r");
  18.  
  19. if (fp == NULL)
  20. {
  21. perror ("Error opening file");
  22. exit(1);
  23. }
  24. for(k=0;k<270;k++)// loop for no. of the rows
  25. {
  26. i=0;
  27. m=0;
  28. line[0] = 0;
  29. fgets(line,nLineSize,fp);
  30. row_cnt++;
  31. puts (line);
  32. while(line[i]!='\n'&&m<13)
  33. {
  34. while(line[i]!=' ')
  35. {
  36. symptom[j]=line[i];
  37. j++;
  38. i++;
  39. }
  40. val=atof(symptom);
  41. example[m][k]=val;
  42. m++;
  43. i++;
  44. j=0;
  45. }
  46. }
  47.  
  48.  
  49. symptom[0]=line[i];
  50. val=atof(symptom);
  51. //cout<<val;
  52. example[m][k]=val;
  53. cout<<"total rows:="<<row_cnt<<endl;
  54.  
  55. for( i=0;i<270;i++)
  56. {
  57. for(j=0;j<13;j++)
  58. cout<<example[j][i]<<" ";//display the array
  59.  
  60. cout<<"\n";
  61. }
  62.  
  63. }

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>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: How to copy floating point values from text file to array

 
0
  #5
Mar 8th, 2008
use const int values to define the array dimensions then use those variables in the code. Or you could do something like this:
  1. int rows = sizeof( example ) / sizeof( example[0] );
  2. int cols = sizeof( example[0] ) / sizeof(float);
  3. for(k=0;k< rows ;k++) // line 24
  4. {
  5.  
  6. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC