Problem in writing string to File

This is a discussion on Problem in writing string to File within the C Programming forums, part of the General Programming Boards category; Hi , I am writing a code for writing a whole string to file,but it is running in infinte loop ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Problem in writing string to File

    Hi ,

    I am writing a code for writing a whole string to file,but it is running in infinte loop ??
    Means the final string is written infinte times.

    The code is:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    typedef struct file
    {
      char send_mail[20];
      char smtp[20];
      char to[50];
      char from[50];
    };
    
    int main()
    {
      file *ptr;
      FILE *fptr;
      ptr = (struct file *)malloc(sizeof(file));
    
    
      strcpy(ptr->send_mail,"send_smtp_mail ");
        strcpy(ptr->smtp,"127.0.0.1 ");
        strcpy(ptr->to,"bargi@abc.com ");
         strcpy(ptr->from,"bargi@abc.com ");
     
    
      
       strcat(ptr->send_mail,ptr->smtp);
          strcat(ptr->send_mail,ptr->to);
          strcat(ptr->send_mail,ptr->from);
            strcat(ptr->send_mail,"\n");
       printf("\n%s\n",ptr->send_mail);
    
       fptr = fopen("/home/bargi/malloc/tmp.txt","w");
       if (fptr == NULL)
       printf("NULL");
       while(ptr->send_mail != "\n")
       {
         fputs(ptr->send_mail,fptr);
        }
        fclose(fptr);
    }
    Can anyone explain why the loop is running infinte..

    Thanks
    Last edited by Bargi; 02-18-2009 at 11:46 AM. Reason: more expalination

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you can't compare strings without a function either.
    Code:
    while(strcmp(ptr->send_mail, "\n") != 0)
    {
         fputs(ptr->send_mail,fptr);
    }
    but when you just compare a string with another one character string, you can do away with the function call and check the character directly.
    Code:
    while(ptr->send_mail[0] != '\n')
    {
         fputs(ptr->send_mail,fptr);
    }

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Still not working...

    Giving segmentation fault

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are strcat'ing many more than 20 characters into "send_mail" string, aren't you?

    --
    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.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Increased the size but result is same....??
    Should I write single character and iterate whole string??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21