Thread: How to call string from different function in C?

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    22

    Post How to call string from different function in C?

    So this creates a file, stores some text and presents a menu. Currently only 1 option is to censor the file. I created read_string to open the file and take the text from there. So I want to censor the text from that file but it doesn't seem to call from read_string.

    insert
    Code:
    
    
    
    char read_string(char *fname, char *sentence){
        printf("Enter the filename:");
        scanf("%s",fname);
        
        FILE* ptr = fopen(fname, "r");
     
        if (NULL == ptr) {
            printf("file can't be opened \n");
        }
     
        printf("content of this file are \n");
     
        while (!feof(ptr)) {
            char sentence1 = fgetc(ptr);
            printf("%c", sentence);
        }
        fclose(ptr);    
    }
    
    
    
    
    void store_file(){
        char str[1000];
        FILE *fptr;
        char fname[200];
        char sentence[2000];
        
        printf("Input file path:");
        scanf("%s",fname);
        fflush(stdin);
        
        fptr=fopen(fname,"w");    
        if(fptr==NULL)
           {
              printf(" Error in opening file!");
              exit(1);
           }
        printf(" Input a sentence for the file : ");
        fgets(str, sizeof str, stdin);
        fprintf(fptr,"%s",str);
        fclose(fptr);
        printf("\n The file %s created successfully...!!\n\n",fname);
        
        fflush(stdin);
        
    }
       
       
    char *censor(char *sentence, char *word){
        char *p;
        for (p = sentence; (p = strstr(p, word)) != NULL; )
            while (isalpha(*p))
                *p++ = '*';
         printf("%s\n", sentence);
        
    }
    
    
    void censor_main(){
        
        char fname[500];
        char sentence[1024];
        
        read_string(fname,sentence);
        fgets(sentence, sizeof sentence, stdin);
        int len = strlen(sentence);
        if (len && sentence[len - 1] == '\n') sentence[--len] = '\0';
         
         
        printf("Enter a word to censor: ");
        char word[128];
        fgets(word, sizeof word, stdin);
        len = strlen(word);
        if (len && word[len - 1] == '\n') word[--len] = '\0';
          censor(sentence,word);
          
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well read_string isn't actually storing anything in sentence.
    Code:
    char read_string(char *fname, char *sentence){
        printf("Enter the filename:");
        scanf("%s",fname);
         
        FILE* ptr = fopen(fname, "r");
      
        if (NULL == ptr) {
            printf("file can't be opened \n");
            // you should return here with an error, not carry on.
        }
      
        // mis-information, you're meant to be reading the file, not printing it.
        // stick to the purpose at hand.
        printf("content of this file are \n");
      
        int nchars = 0;
        while (!feof(ptr)) {  // see the FAQ link below
            char sentence1 = fgetc(ptr);
            // now store it
            sentence[nchars++] = sentence1;  // awful name for a single char
            // printf("%c", sentence);
        }
        sentence[nchars] = '\0';  // make it a proper string
        fclose(ptr);    
        // return success?
    }
    Even if it did, you immediately overwrite it with line 66.


    Read this -> A development process
    You keep trying to do too much without checking what you have so far even works.
    So you end up with programs broken with multiple issues and don't know what to do next.

    Also this
    Why fflush(stdin) is wrong - Cprogramming.com
    and this
    Why it's bad to use feof() to control a loop - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string to call function
    By s002wjh in forum C Programming
    Replies: 2
    Last Post: 03-22-2018, 08:27 PM
  2. Replies: 11
    Last Post: 05-19-2009, 08:17 AM
  3. Can U Use a string to call a function
    By passy in forum C Programming
    Replies: 3
    Last Post: 08-11-2003, 10:15 AM
  4. Changing a input string to a function call?
    By cezaryn in forum C Programming
    Replies: 4
    Last Post: 07-16-2003, 02:37 PM
  5. Call a function from a string?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-06-2002, 04:38 PM

Tags for this Thread