Thread: function problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    function problem

    Hey i m trying to code a function that reads txt and change them from normal words to character arrays like NAME becomes "NAME" my function work yes but problem is that in c i dont think that " " can become visible any suggestion ?
    Code:
    #include <stdio.h>
    #include <string.h>
    #define FNAME  "D:\\test\\lol.txt"
    void Copy_it(char *buffer,char *save)
    {
         int i;
         for(i=0;save[i]=buffer[i];i++);
    }
    void add_it(char *buffer,char *save)
    {
         int i;
         Copy_it(buffer,save);
         strcat(buffer,"'");
         strcat(buffer,save);
         strcat(buffer,"'");
    }
    int main(void)
    {
        char buffer[100];
        char save[100];
        FILE *fp;
        fp=fopen(FNAME,"r+w");
        while(fgets(buffer,sizeof buffer,fp)!=NULL) {
             add_it(buffer,save);
             puts(buffer);
             }
        getchar();
        return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean you want to add " at either end of your string?

    If so, you could use strcpy(buffer, "\""); and similarly in the strcat() function.

    You may want to remove the newline from the string out of fgets() tho', as discussed (twice) in recent posts.

    I would perhaps also cheat and read the string into position 1 of a string, and reduce the size given to fgets to store leave a space at the end as well, so you could do:
    Code:
    char buffer[102];
    int len;
    
    buffer[0] = '\"';
    fgets(&buffer[1], sizeof(buffer)-2, stdin);
    len = strlen(buffer);
    if (buffer[len-1] == '\n') len--;
    buffer[len] = '\"';
    Saves making a copy of the string, only to add a single character at either end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i dont think that " " can become visible

    I don't understand what you mean there, can you elaborate. Also, it looks like you're just adding the text in quotes to the end of the unquoted text. You probably want the first strcat to be a strcpy. Come to think of it, why are you using Copy_it when strcpy does the same thing?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah thanks matsp also sebsiti thanks also i will use strcpy
    Code:
    #include <stdio.h>
    #include <string.h>
    #define FNAME  "D:\\test\\lolz.txt"
    #define FOUT   "D:\\test\\out.txt"
    #define DEL_IT(P,N)P[N]=0
    void Copy_it(char *buffer,char *save)
    {
         int i;
         for(i=0;save[i]=buffer[i];i++);
    }
    void add_it(char *buffer,char *save)
    {
         int i;
         char *p;
         Copy_it(buffer,save);
         DEL_IT(buffer,0);
         strcat(buffer,"\"");
         strcat(buffer,save);
         if(p=strchr(buffer,'\n'))
             *p='\"';
         strcat(buffer,",");
    }
    int main(void)
    {
        char buffer[100];
        char save[100];
        FILE *fp,*fp2;
        fp=fopen(FNAME,"r");
        fp2=fopen(FOUT,"w");
        if(!fp && !fp2) {
           fclose(fp);
           fclose(fp2);
           puts("problem happened exiting now");
           getchar();
           return 0;
           }
        while(fgets(buffer,sizeof buffer,fp)!=NULL) {
             add_it(buffer,save);
             fprintf(fp2,"%s",buffer);
             }
        getchar();
        fclose(fp);
        fclose(fp2);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM