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 7176537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:31:44+00:00 2026-05-28T16:31:44+00:00

having some issues with a networking assignment. End goal is to have a C

  • 0

having some issues with a networking assignment. End goal is to have a C program that grabs a file from a given URL via HTTP and writes it to a given filename. I’ve got it working fine for most text files, but I’m running into some issues, which I suspect all come from the same root cause.

Here’s a quick version of the code I’m using to transfer the data from the network file descriptor to the output file descriptor:

unsigned long content_length; // extracted from HTTP header
unsigned long successfully_read = 0;
while(successfully_read != content_length)
{
  char buffer[2048];
  int extracted = read(connection,buffer,2048);
  fprintf(output_file,buffer);
  successfully_read += extracted;
}

As I said, this works fine for most text files (though the % symbol confuses fprintf, so it would be nice to have a way to deal with that). The problem is that it just hangs forever when I try to get non-text files (a .png is the basic test file I’m working with, but the program needs to be able to handle anything).

I’ve done some debugging and I know I’m not going over content_length, getting errors during read, or hitting some network bottleneck. I looked around online but all the C file i/o code I can find for binary files seems to be based on the idea that you know how the data inside the file is structured. I don’t know how it’s structured, and I don’t really care; I just want to copy the contents of one file descriptor into another.

Can anyone point me towards some built-in file i/o functions that I can bludgeon into use for that purpose?

Edit: Alternately, is there a standard field in the HTTP header that would tell me how to handle whatever file I’m working with?

  • 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-05-28T16:31:45+00:00Added an answer on May 28, 2026 at 4:31 pm

    You are using the wrong tool for the job. fprintf takes a format string and extra arguments, like this:

    fprintf(output_file, "hello %s, today is the %d", cstring, dayoftheweek);
    

    If you pass the second argument from an unknown source (like the web, which you are doing) you can accidentally have %s or %d or other format specifiers in the string. Then fprintf will try to read more arguments than it was passed, and cause undefined behaviour.

    Use fwrite for this:

    fwrite(buffer, 1, extracted, output_file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having some issues running my code local. I have host file setup like this:
I have been having some issues retrieving JSON data from a WCF service application
Having some issues accessing my param tags from within actionscript. Essentially I have the
I'm having some issues returning unique results from a table that has some repeating
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
I am having some issues with managers, because they believe that (intuitively) creating desktop
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
I'm having some issues deploying a Visio addin. Running the VSTO file works on
I am having some issues creating a setup file for my Outlook 2007 add-in.
Having some issues with C. I have this is my code: // First line

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.