Thread: strange edffect of "fputs"

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    strange edffect of "fputs"

    Hi friends,

    I am stuck with little problem, the program reads a file and stores
    in str1, then I put a loop that extracts 4 words at time by using a counter, thanks to Mr.nutshell who gave me a hint how to read in 4 word at a time, then the program stores the extracted 4 words int an array called pboxOut .. and then a function b_pox is called which swaps the two middle characters and finally the function return is read to file called outfile

    The problem is that the output is written to the file but strange
    characters are put at the back of the string written to the file.
    I am using fputs to write to a file.

    can anyone see what may be the cause of this strange characters at the back of thr string.

    Code:
    code
    #define max 1024
    char* p_box (char* orig_msg , char* msg_after_pbox ) ;
    
    int main (int argc, char *argv[])
    {
        FILE *infile, *outfile;
        //char ch;
        char str1 [max];
        char pboxOut [5];  /* temporary for output */   
        int j,count;
        char Temp1[5];
      /* start processing the file  */
       while( fgets(str1,max,infile) != NULL ) 
       {               
              count = 0;
              for (j = 0 ; j < max; j++)
              {
                  /* if count = 4, we already have letters,start processing */
                  if (count % 4 == 0)
                  {
                     /* check if what read is character */
                    if (isalpha(str1[j])) 
                       Temp1 [0] = str1[ j ];
                       Temp1 [1] = str1[ j + 1];
                       Temp1 [2] = str1[ j + 2];
                       Temp1 [3] = str1[ j + 3];
                       Temp1 [4] = '\0';
                       
                       p_box( Temp1, pboxOut );          
                   
                       fputs(pboxOut,outfile);
    
                       //fprintf(outfile,pboxOut);
    
                       count = 0;
                    }
                  
                    count++; 
          }
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The standard says about fgets: No additional characters are read after a new-line character (which is retained) or after end-of-file. So you can only read one line with fgets.

    How is the data in your file organised? If each line contains 4 characters, then you could do something like:

    Code:
        while (not end of file)
        {
            if (fgets (string, max, infile) != 0)
            {
                /* Assume it are all characters */
                all_characters = true;
                
                /* Check if it are all characters */
                for (i = 0; i < 4 && all_characters == true; i++)
                {            
                    if (!isalpha (string [i]))
                        /* A non-character is found */
                        all_cahracters = false;
                }
            
                if (all_characters == true)
                {                
                    p_box (string, pbox_out);
                
                    fputs (pbox_out, outfile);
                }
            }
        }
    Note that max should be 4 here, since you want to read at most 4 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM