Problem with translating data with comma to array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 11
Reputation: ahjiefreak is an unknown quantity at this point 
Solved Threads: 1
ahjiefreak ahjiefreak is offline Offline
Newbie Poster

Problem with translating data with comma to array

 
0
  #1
Mar 20th, 2008
Hi,

Recently I have a text file which contain a bunch of numbers :-

1,0,0,1,0................
1,0,1,1,1................

I would like to read these numbers line by line and stored into an array.

However, I get seg fault for this.
  1. char filename[5000];
  2. char dump;
  3.  
  4. strcpy(filename,argv[2]);
  5.  
  6.  
  7. while(!feof(tfPtr))
  8. {
  9. for (i=0; i<16; i++)
  10. {
  11. fscanf(filename, "%d", t[i]);
  12. fscanf(filename, "%c", dump);
  13. }
  14. sum=t[3]+t[5];
  15. sum2=t[2]+t[4];
  16. }
  17. fclose(tfPtr);
  18.  
  19. exit(0);
When I compile together with filename
./test.exe test.txt
Segmentation fault (core dumped)



Please help or you guys have any other suggestion ? Thanks.
Last edited by Narue; Mar 20th, 2008 at 1:24 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Problem with translating data with comma to array

 
0
  #2
Mar 20th, 2008
You could try reading the whole file into memory and store it in a character array i.e. char*. Then you could use the function strtok() to "tokenise" the character array, i.e. split up the character array using the delimiters you specify which in your case would be commas etc.. I'd suggest you read up and try your hand with strtok(). If you need it I can give you a little example of how to do this..

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,843
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Problem with translating data with comma to array

 
0
  #3
Mar 20th, 2008
>fscanf(filename, "%d", t[i]);
>fscanf(filename, "%c", dump);
Don't forget that scanf expects a pointer. If your variable isn't already a pointer, you need to prefix it with the address-of operator:
  1. fscanf(filename, "%d", &t[i]);
  2. fscanf(filename, "%c", &dump);
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 11
Reputation: ahjiefreak is an unknown quantity at this point 
Solved Threads: 1
ahjiefreak ahjiefreak is offline Offline
Newbie Poster

Re: Problem with translating data with comma to array

 
0
  #4
Mar 20th, 2008
Hi,

If i assign a pointer based, it would have meant the filename is always fix at all time. I desired something in the end of the day which executes whole bunch of different test.txt namely test.txt, test1.text...etc. If that is the case, I declare a string argument inside to get the filename.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *strtok(char *str1, const char *str2);
  6.  
  7. #define SIZE 16
  8.  
  9. main (int argc, char *argv[])
  10. {
  11.  
  12. FILE *cfPtr; /* for reading data to expand.txt for vector based */
  13. FILE *tfPtr; /* for reading data from svmkb.txt */
  14.  
  15. int nt[SIZE];
  16. int subs[7];
  17. int i;
  18. char *filename;
  19. char *result=NULL;
  20. char input[5000];
  21. char delims=";";
  22. strcpy(filename,argv[2]);
  23.  
  24. tfPtr = fopen(filename,"rt");
  25. if (tfPtr == NULL)
  26. {
  27. printf("Can't find input file %s",filename);
  28. exit(-1);
  29. }
  30.  
  31. fgets(input,SIZE,tfPtr);
  32. if (input == NULL)
  33. {
  34. printf("Error on initial read from universe file.\n");
  35. fclose(tfPtr);
  36. exit(-1);
  37. }
  38.  
  39.  
  40. while(!feof(tfPtr))
  41. {
  42. result =strtok(input, delims);
  43.  
  44. for (i=0; i<16; i++)
  45. {
  46. fscanf(tfPtr, "%d", &result[i]);
  47.  
  48. nt[i]=result[i];
  49.  
  50. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,843
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Problem with translating data with comma to array

 
0
  #5
Mar 21st, 2008
>strcpy(filename,argv[2]);
You haven't allocated any memory to filename. An uninitialized pointer is not the same thing as a pointer to infinite memory. You're better off just using an array instead of a pointer, especially in the case of a file name where you can figure out the maximum length of a valid path.

>tfPtr = fopen(filename,"rt");
I'm curious. Why not just use argv[2] instead of juggling with the filename pointer?

>#define SIZE 16
>char input[5000];
>fgets(input,SIZE,tfPtr);
That seems like kind of a waste to me. You give input 5000 characters but only utilize 15 of them for data.

>nt[i]=result[i];
result is NULL at this point; you'll likely get a segmentation fault.
New members chased away this month: 4
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



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

©2003 - 2009 DaniWeb® LLC