Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:38:36+00:00 2026-06-01T23:38:36+00:00

I am working on some kind of file sharing program which is written in

  • 0

I am working on some kind of file sharing program which is written in C. There is a function that can read a data file and store the data into a string and return this string to main function and the main function send back to client. Codes are shown below

char* ListFiles(){
    FILE *fp;
    char file[30];
    char *f;
    if((fp=fopen("list","r"))==NULL)  
    {
        ...
    }
    while (!feof(fp))
    {
        fgets(file,50,fp);
    }
    fclose(fp);
    f=file;
    printf("%s",f); //get display!!!
    return f;
}
int main(){
      char *files;
      ...
      ...
      files=ListFiles();
      printf("%s",files); //nothing display!!
      sent();
}

However, this method doesn’t work. There is nothing display and of course nothing is sent. But I do get the correct display in function ListFiles(). I don’t know what happen. I also use strcpy() and it still fail to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:38:37+00:00Added an answer on June 1, 2026 at 11:38 pm

    Follow George Skoptsov recommendations. But If you don’t have the strdup() function,then use this:

    char* strdup(const char* org)
    {
        if(org == NULL) return NULL;
    
        char* newstr = malloc(strlen(org)+1);
        char* p;
    
        if(newstr == NULL) return NULL;
    
        p = newstr;
    
        while(*org) *p++ = *org++; /* copy the string. */
        return newstr;
    }
    

    And then:

    #include <string.h> /* strlen() call */
    #include <stdlib.h> /* NULL, malloc() and free() call */
    
    /* do something... */
    
    char* ListFiles() {
            /* .... */ 
             return strdup(f);
    }
    

    Or instead of char file[30]; do a dynamic memory allocation: char* file = malloc(30); then you can do return f; and it will work fine because f now is not a pointer to a local variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I read once that google chrome was working on some kind of
I have some kind of reader which only has a handle (FILE*) to a
I'm working on a program that modifies a file, and I'm wondering if the
I'm working some code that inserts csv rows into an SQLite database using Python.
Working on some tweaks for a build script, I noticed that the output from
While working with some sources written in C++ I found those macro: JNIEXPORT return_type
When working on some Javascript for a web application, I noticed that I had
I'm working on a system that requires high file I/O performance (with C#). Basically,
Is there a way in C#, perhaps using an external library of some kind,
I have made a simple program with : working with files(read write) end class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.