Facing issue with fgets in while loop

This is a discussion on Facing issue with fgets in while loop within the C Programming forums, part of the General Programming Boards category; I have a program like below Code: #define LEN 4048 FILE *ptr; char line[LEN]; fp=popen(cmd,"r"); while(fgets(line,LEN,fp)!=NULL) { printf("%s",line); strcat(dest,line); } ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Facing issue with fgets in while loop

    I have a program like below
    Code:
    #define LEN 4048
    
    FILE *ptr;
    char line[LEN];
    fp=popen(cmd,"r");
    
    while(fgets(line,LEN,fp)!=NULL)
    {
      printf("%s",line);
       strcat(dest,line);
    }
    
    printf("%s",dest);
    i want to call a server program using the command line argument passed to this popen().

    if i pass the cmd as -s(server), it fetches the server data and storing that in dest buffer.
    i want to print the data received in "dest" outside the loop(while loop never exit for server)

    Please help to print the data out of the while loop.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    That code is not a program. To be a working program, the code would need to be in functions.

    Look up the form of the main() function (a function that the programmer must supply for every program). The arguments for that function allow retrieving of command line arguments.

    Also look up the documentation for fgets(). It has specific behaviours if reading a line that is longer than the specified buffer length (LEN in your case). It also leaves a '\n' character in the buffer in most circumstances.

    If you are going to do strcat(dest, line) then dest must be defined - and it is not in your "code". If dest is an array, then the length of that array must be sufficient to allow line to be appended to it. If dest is a pointer, it must point at the first character of a buffer that is long enough to allow line to be appended to it.

    If my description is unclear to you, then you need to stick your nose into a basic text on C. Both the nature of your "code" and the way you have asked your question suggest you are just hacking, rather than understanding what you are doing.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Quote Originally Posted by grumpy View Post
    That code is not a program. To be a working program, the code would need to be in functions.

    Look up the form of the main() function (a function that the programmer must supply for every program). The arguments for that function allow retrieving of command line arguments.

    Also look up the documentation for fgets(). It has specific behaviours if reading a line that is longer than the specified buffer length (LEN in your case). It also leaves a '\n' character in the buffer in most circumstances.

    If you are going to do strcat(dest, line) then dest must be defined - and it is not in your "code". If dest is an array, then the length of that array must be sufficient to allow line to be appended to it. If dest is a pointer, it must point at the first character of a buffer that is long enough to allow line to be appended to it.

    If my description is unclear to you, then you need to stick your nose into a basic text on C. Both the nature of your "code" and the way you have asked your question suggest you are just hacking, rather than understanding what you are doing.

    I missed the dest declaration, its char dest[LEN].I am not hacking,i have shared only the part of code where i face this issue.

    I need to execute the "iperf -s" command.Since the Server waits for receiving data from client, in my while loop also, it waits to receive data.

    If there is any alternate method to get data, please suggest.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's hard to understand what exactly your question is. If you could post some code that actually runs (or as close as you can get to it) that might clarify things. In general the incomplete code you posted looks correct. Make sure you initially zero-terminate dest so the the first strcat works properly.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    Quote Originally Posted by pingloo02 View Post
    I missed the dest declaration, its char dest[LEN].I am not hacking,i have shared only the part of code where i face this issue.
    Yeah, well.

    You have only shared code that you think is relevant to the problem. The code you have shared is insufficient.

    If you can't isolate the problem in your own code, and then you provide only a "part" of the code, there's a good chance you will remove something relevant to your problem without knowing it. That requires anyone reading your post to be mindreaders .... and most forum members are not.

    Try recreating your specific problem in a small but complete sample of compilable code. In the process of creating such a sample you might have an "aha!" moment. If not, you will be able to post code that actually exhibits your problem, with a useful description, and that will allow people to help you.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing using fgets issue
    By ccc123 in forum C Programming
    Replies: 3
    Last Post: 05-30-2011, 11:41 AM
  2. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 07:15 PM
  3. fgets issue
    By Fox101 in forum C Programming
    Replies: 2
    Last Post: 05-05-2008, 10:11 PM
  4. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 10:26 AM
  5. fgets in while loop
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 05-02-2005, 04:42 AM

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