943,724 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1160
  • C RSS
Mar 20th, 2008
0

Problem with translating data with comma to array

Expand Post »
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ahjiefreak is offline Offline
11 posts
since Dec 2007
Mar 20th, 2008
0

Re: Problem with translating data with comma to array

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.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Mar 20th, 2008
0

Re: Problem with translating data with comma to array

>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);
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 20th, 2008
0

Re: Problem with translating data with comma to array

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. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ahjiefreak is offline Offline
11 posts
since Dec 2007
Mar 21st, 2008
0

Re: Problem with translating data with comma to array

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: urgent
Next Thread in C Forum Timeline: printing problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC