943,793 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 18174
  • C RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Sep 25th, 2006
1

Re: use of scanf()

also the program is showing no compilation error, onlyruntime segmentation error is there...
i am working in linux
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 25th, 2006
1

Re: use of scanf()

> scanf("%s", &retry);
First buffer overflow - retry is a single char, you can't put a string into a single char.
Second problem, calling a function recursively just to implement a while loop is poor form.
Third, you're mixing scanf() with fgets(), which will surely lead to trouble at some point.

Separate the function into two steps
- readInput
- TokeniseInput.

Both will be a lot simpler than what you have at the moment.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 26th, 2006
1

Re: use of scanf()

So i will have to declare my variables global..
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 26th, 2006
1

Re: use of scanf()

it didn't work . i think the segmentation error is because of some logical error not because of scanf(), gets() problem
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 26th, 2006
1

Re: use of scanf()

Click to Expand / Collapse  Quote originally posted by rati ...
it didn't work . i think the segmentation error is because of some logical error not because of scanf(), gets() problem
Thats why we insist to post the code or if you cant, then U need to reconstruct the problem in smaler code. The post I posted don't give seg error, if U don't belive me compile and run the code which I posted.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 27th, 2006
0

Re: use of scanf()

  1. //# include<stdio.h>
  2. void function();
  3. main()
  4. {
  5. function();
  6. }
  7. void function()
  8. {
  9. int i,index,k,m;
  10. char str1[100],retry,str[100],*ptr,*ctr;
  11. int j=0;
  12. printf("enter the string");
  13. fgets(str, sizeof(str), stdin);
  14. if((ptr = strchr(str, '\n')) != NULL)
  15. *ptr = '\0';
  16. printf("enter the string to be compared");
  17. fgets(str1, sizeof(str1) , stdin);
  18. if((ctr = strchr(str1, '\n')) != NULL)
  19. *ctr = '\0';
  20. /* printf("enter index"); */
  21. /* canf("%d",& index); */
  22. char *ch,*pch;
  23. ch=strtok(str,":");
  24. printf(" ratg");
  25. if (ch==NULL)
  26. {
  27. printf("entered string is incorrect,reenter? y/n");
  28. scanf("%s",&retry);
  29. if(retry=='y')
  30. function();
  31. else return;
  32. }
  33. else
  34. printf("\nch is NULL\n");
  35. while(ch!=NULL)
  36. {
  37. pch=strstr(ch,";");
  38. i=pch-ch;
  39. /* strncpy(tr[j].member1,ch,i); */
  40. /* strcpy(tr[j].member2,pch+1); */
  41. j++;
  42. ch=strtok(NULL,":");
  43. }


i tried your code . yes it is not giving any segmentation error but it is not performing desired operation
first of all it is giving a warning:
assignment makes pointer from integer without a cast
for statements 23 and 42
then it is not going inside the loop for asking the user if he wants to enter again the string

i am working on linux , i hope this is not creating any problems

i will be posting a reconstructed program of mine for your convinience
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 27th, 2006
0

Re: use of scanf()

  1. // # include<stdio.h>
  2. void function();
  3. main()
  4. {
  5. function();
  6. }
  7. void function()
  8. {
  9. int i,index,k,m;
  10. char str1[100],retry,str[100],*ptr,*ctr;
  11. int j=0;
  12. printf("enter the string");
  13. fgets(str, sizeof(str), stdin);
  14. if((ptr = strchr(str, '\n')) != NULL)
  15. *ptr = '\0';
  16. printf("enter the string to be compared");
  17. fgets(str1, sizeof(str1) , stdin);
  18. if((ctr = strchr(str1, '\n')) != NULL)
  19. *ctr = '\0';
  20. /* printf("enter index"); */
  21. /* canf("%d",& index); */
  22. char *ch,*pch;
  23. ch=strtok(str,":");
  24. printf(" ratg");
  25. if (ch==NULL)
  26. {
  27. printf("entered string is incorrect,reenter? y/n");
  28. scanf("%s",&retry);
  29. if(retry=='y')
  30. function();
  31. else return;
  32. }
  33. else
  34. printf("\nch is NULL\n");
  35. while(ch!=NULL)
  36. {
  37. pch=strstr(ch,";");
  38. i=pch-ch;
  39. /* strncpy(tr[j].member1,ch,i); */
  40. /* strcpy(tr[j].member2,pch+1); */
  41. j++;
  42. ch=strtok(NULL,":");
  43. }
  44. }



i tried your code but it is not giving desired result, it is not asking the user if he wants to reenter
also there are warnings for statements 23 and 42
assignment makes pointer from integer without a cast

i am working on linux , i hope that is not causing any problem

i am working on reconstruction of the code for your convinience
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 27th, 2006
0

Re: use of scanf()

  1. //# include<stdio.h>
  2. # include <string.h>
  3. # include<stdlib.h>
  4. void function(char *);
  5. int j;
  6. struct test
  7. {
  8. char member1[20];
  9. char member2[20];
  10. };
  11. struct test *tr;
  12. int main()
  13. {
  14. char str[100];
  15. printf("enier the string");
  16. scanf("%s",&str);
  17. tr=(struct test *)malloc( sizeof (struct test)*100);
  18. function(str);
  19. free(tr);
  20. return 0;
  21. }
  22. void function(char * str)
  23. {
  24. long int i,index,k;
  25. char str1[100],*ch,*pch;
  26. j=0;
  27. ch=strtok(str,":");
  28. if (ch==NULL)exit(0);
  29. while(ch!=NULL)
  30. {
  31. pch=strstr(ch,";");
  32. i=pch-ch;
  33. strncpy(tr[j].member1,ch,i);
  34. strcpy(tr[j].member2,pch+1);
  35. j++;
  36. ch=strtok(NULL,":");
  37. };
  38. for(k=0;k<j;k++)
  39. {
  40. printf("\n%s",tr[k].member1);
  41. printf("\n%s",tr[k].member2);}
  42. }
this is my code
it gives desired output when string entered is nghg;jhjkh:kjhkh;mhjkh
but gives segmentatiopn error for hjghjgjg
it should exit from the program for such a string
Last edited by rati; Sep 27th, 2006 at 3:36 am. Reason: code written in incorrect way
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 27th, 2006
1

Re: use of scanf()

The seg fault is becouse if you dont have ';' then pch is NULL. So seg fault will happen only if you dont have ';' in the input string. You need to change the logic
  1. #include<stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. void function(char *);
  5. int j;
  6. struct test
  7. {
  8. char member1[20];
  9. char member2[20];
  10. };
  11. struct test *tr;
  12. int main()
  13. {
  14. char str[100], *ptr;
  15. printf("enter the string: ");
  16. /* scanf("%s",&str); */
  17. fgets(str, sizeof(str), stdin);
  18. if((ptr = strchr(str, '\n')) != NULL)
  19. *ptr = '\0';
  20. tr=(struct test *)malloc( sizeof (struct test)*100);
  21. function(str);
  22. free(tr);
  23. return 0;
  24. }
  25. void function(char * str)
  26. {
  27. long int i,index,k;
  28. char str1[100],*ch = NULL,*pch = NULL;
  29. j=0;
  30. tr->member1[0] = '\0';
  31. tr->member2[0] = '\0';
  32.  
  33. ch=strtok(str, ":");
  34. printf("*ch = %c\n", *ch); /* debug print */
  35. if (ch==NULL)
  36. exit(0);
  37. while(ch!=NULL)
  38. {
  39. pch=strstr(ch,";");
  40. if (pch == NULL)
  41. {
  42. ch=strtok(NULL,":");
  43. continue;
  44. }
  45. printf("*pch = %c\n", *pch); /* debug print */
  46. i=pch-ch;
  47. printf("i = %ld\n", i);
  48. strncpy(tr[j].member1,ch,i);
  49. strcpy(tr[j].member2,pch+1);
  50. j++;
  51. ch=strtok(NULL,":");
  52. }
  53. for(k=0;k<j;k++)
  54. {
  55. printf("\n%s",tr[k].member1);
  56. printf("\n%s",tr[k].member2);
  57. }
  58. }
Run this
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 27th, 2006
1

Re: use of scanf()

Right now i am testing for ch, and before pch tests for ; ch is null
so the program should end there itself when ch is null, why would it continue till pch??
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006

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: file compression prgogram
Next Thread in C Forum Timeline: deamon processes





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


Follow us on Twitter


© 2011 DaniWeb® LLC