Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham

  • 3 years ago

can any1 pls check this code and tell if ther's anythin wrong?

  • This Question is Closed
  1. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    void copy(char **array,char name[10]) { FILE *fp; fp=fopen(name,"r"); char c; int i=0,j=0,flag=0,m=0; while((c=getc(fp))!='\n') { if(c==' ') { flag++; if(flag==1) { m=0; array[i][j]=' '; j++; } else if(flag==3) { j=0; array[i][j]='\0'; i++; flag=0; fseek(fp,-m,1); } } else { array[i][j]=c; j++; } m++; printf("%c %c\n",array[i][j],c); } fclose(fp); return; }

  2. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it is supposes to copy groups of three words into a 2d array... @farmdawgnation

  3. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ex: this is a test file this is a is a test a test file

  4. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Are you seeing a problem with the code as is? Knowing what's wrong would help.

  5. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    like array[i][j] and c prints different characters and array[i][j] prints garbage values

  6. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this is the total code if u wanna run #include<stdio.h> #include<string.h> #include<stdlib.h> //int length(char name[10]); char** makeArray(int n); void copy(char **,char name[10]); //void compare(char **array,char name[10],int x); //int count(char name[10]); main() { int k=0,x; char name[10]; printf("enter the name of the file:"); scanf("%s",name); int i,j,comp[10]; char **array; x=length(name); array=makeArray(x); copy(array,name); /*for(j=0;j<x/1000;j++) { printf("%s\n",array[j]); } /* compare(array,name,x);*/ } int length(char name[10]) { int x=0,max=0,j=0,flag=0,m; FILE *fp; fp=fopen(name,"r"); char c; while((c=getc(fp))!='\n') { // printf("flag:%d,m:%d,j:%d,max:%d,char:%c,x:%d\n",flag,m,j,max,c,x); if(c==' ') { flag++; if(max<j) max=j; if(flag==1) { m=0; } if(flag==3) { x++; if(max<j) max=j; j=0; flag=0; fseek(fp,-m,1); } } else { j++; } m++; } fclose(fp); x=x+1; printf("the number of phraes:%d,max size of word:%d\n",x,max); x=x*1000+max; printf("%d\n",x); return 1000*(x)+max ; } char** makeArray(int n) { int i; char **array; array=(char **)malloc((n/1000)*(sizeof(char *))); for(i=0;i<n;i++) { array[i]=(char *)malloc((n%1000)*sizeof(char)); } return array; } void copy(char **array,char name[10]) { FILE *fp; fp=fopen(name,"r"); char c; int i=0,j=0,flag=0,m=0; while((c=getc(fp))!='\n') { if(c==' ') { flag++; if(flag==1) { m=0; array[i][j]=' '; j++; } else if(flag==3) { j=0; array[i][j]='\0'; i++; flag=0; fseek(fp,-m,1); } } else { array[i][j]=c; j++; } m++; printf("%c %c\n",array[i][j],c); } fclose(fp); return; }

  7. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You're never allocating memory for array for starters.

  8. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ah.

  9. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i did and passed the array as an argument r8

  10. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yowza segmentation fault.

  11. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You're doing something you're not supposed to :P

  12. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i dint get any segmentation fault

  13. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ``` enter the name of the file:testfile the number of phraes:3,max size of word:7 3007 ? t 2 h ? i ? s i s , a i s a t e s t a t e s t f i l e Segmentation fault: 11 ```

  14. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    BTW. Single letter variable names suck. :P

  15. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah man, I'm not sure what's going on. I don't really feel like spending three hours with gdb digging into it to figure it out. I don't really have that much free time atm. :/

  16. farmdawgnation
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    FWIW I don't see anything obvious.

  17. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hmmm thanks anyway....if u find anythin lemme kno

  18. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    lol not a bigg thing....the print statement was showing some random values coz.... of j!!! ie the next character that's not copies yet!!

  19. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    nd that xplainds the seg fault too

  20. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy