reading data from text fies in c

Reply

Join Date: Mar 2007
Posts: 4
Reputation: potato_bum is an unknown quantity at this point 
Solved Threads: 0
potato_bum potato_bum is offline Offline
Newbie Poster

reading data from text fies in c

 
0
  #1
Mar 21st, 2007
hi

im asked to read data inputs from a text file in c.

my problem is i dont know to separate the values,

ex. may data contains these:

1.00:2.00:3.00

i would like to assign the values a = 1:00, b = 2.00, c =3.00.

how is it done?

thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,494
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: 1603
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: reading data from text fies in c

 
0
  #2
Mar 21st, 2007
you have a couple options:

1. use strtok() in a loop to extract the individual strings and copy the text into another string array.

2. use a pointer and strchr() to locate each colon then copy the text into another string up to the pointer that was returned by strchr().
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: potato_bum is an unknown quantity at this point 
Solved Threads: 0
potato_bum potato_bum is offline Offline
Newbie Poster

Re: reading data from text fies in c

 
0
  #3
Mar 22nd, 2007
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main ()
  4. {
  5. char num[] ="1.00:2.00:3.00";
  6. char *ch;
  7. char r[100];
  8. printf ("split \"%s\":\n",num);
  9. ch = strtok (num,":");
  10. while (ch != NULL)
  11. {
  12. i = 0;
  13. printf ("%s\n",ch);
  14. ch = &r[i];
  15. ch = strtok (NULL, ":");
  16. i ++;
  17. }
  18. return 0;
  19. }

--> here is my strtok code, but it doesn't work.

how can i copy the values of the pointer ch into a char array r? thanks.
Last edited by ~s.o.s~; Mar 22nd, 2007 at 2:22 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,764
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 492
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: reading data from text fies in c

 
0
  #4
Mar 22nd, 2007
Use [search]strcpy[/search] to copy the contents of the string pointed by ch to another string.
Last edited by ~s.o.s~; Mar 22nd, 2007 at 2:29 pm.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: JeganathanK is an unknown quantity at this point 
Solved Threads: 0
JeganathanK JeganathanK is offline Offline
Newbie Poster

Re: reading data from text fies in c

 
0
  #5
Mar 23rd, 2007
Originally Posted by potato_bum View Post
hi

im asked to read data inputs from a text file in c.

my problem is i dont know to separate the values,

ex. may data contains these:

1.00:2.00:3.00

i would like to assign the values a = 1:00, b = 2.00, c =3.00.

how is it done?

thanks!

This is the code which u want...

/*******************************************/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. FILE *fpInputFilePtr;
  7. float a[3];
  8. char strTokan[] = {":"};
  9. char *strLineInfo = (char*) calloc(256,1);
  10. char *ptrStrLineInfo = strLineInfo;
  11. int nTmpCnt=0;
  12. fpInputFilePtr = fopen("E:\\Test Projects\\Input.txt","r");
  13. while(fgets(strLineInfo,256,fpInputFilePtr))
  14. {
  15. strLineInfo[strlen(strLineInfo)-1]='\0';
  16. while(strlen(strLineInfo)>1)
  17. {
  18. strtok(strLineInfo,strTokan);
  19. a[nTmpCnt]=atof(strLineInfo);
  20. strLineInfo += strlen(strLineInfo)+1;
  21. }
  22. strLineInfo = ptrStrLineInfo;
  23. memset(strLineInfo,'\0',sizeof(strLineInfo));
  24. }
  25. /******** Do your work here*****/
  26.  
  27. if(fpInputFilePtr)
  28. {
  29. fclose(fpInputFilePtr);
  30. fpInputFilePtr=NULL;
  31. }
  32. if(strLineInfo)
  33. {
  34. free(strLineInfo);
  35. strLineInfo=NULL;
  36. }
  37. if(ptrStrLineInfo)
  38. {
  39. free(ptrStrLineInfo);
  40. ptrStrLineInfo=NULL;
  41. }
  42. return 0;
  43. }
/****************************/


If u have any queries feel free to mail to nrk_jegan@yahoo.com

Regards,
Jeganathan Krishnasamy.
Last edited by Ancient Dragon; Mar 23rd, 2007 at 8:40 am. Reason: add missing code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,494
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: 1603
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: reading data from text fies in c

 
0
  #6
Mar 23rd, 2007
Sorry, Jeg, but your code doesn't work either. The lines (16-21) using strtok() are incorrect and will only find the first occurence of the colon and no others. Please test out your program to make sure it works before attempting to show someone else how to write a program.
Last edited by Ancient Dragon; Mar 23rd, 2007 at 8:45 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: JeganathanK is an unknown quantity at this point 
Solved Threads: 0
JeganathanK JeganathanK is offline Offline
Newbie Poster

Re: reading data from text fies in c

 
0
  #7
Mar 26th, 2007
Definitely it'll work.Otherwise i won't put it here.I tested it and working fine.
plz once again check the code by running it or just debug it.I'm incrementing the character pointer.So each time i'm eliminating one ":".
This code is tested for 10 inputs from file.
Last edited by JeganathanK; Mar 26th, 2007 at 1:22 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,494
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: 1603
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: reading data from text fies in c

 
0
  #8
Mar 26th, 2007
here is how that loop should have been coded. Your program has a lot of unnecessary and just plain clumbsy code, such as the way it increments strLineInfo

  1. while(fgets(strLineInfo,256,fpInputFilePtr))
  2. {
  3. if( strLineInfo[strlen(strLineInfo)-1] == '\n')
  4. strLineInfo[strlen(strLineInfo)-1]='\0';
  5. ptrStrLineInfo = strtok(strLineInfo,strToken);
  6. while(ptrStrLineInfo != NULL)
  7. {
  8. a[nTmpCnt]=atof(ptrStrLineInfo);
  9. ++nTmpCnt;
  10. ptrStrLineInfo = strtok(NULL, strTokan);
  11. }
  12. }

>>memset(strLineInfo,'\0',sizeof(strLineInfo));
Since strLineInfo is a pointer the sizeof operator will always return the size of a pointer, which is 4 on most 32-bit compilers. So that memset line is pretty useless.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

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




Views: 1389 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC