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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:13:51+00:00 2026-05-31T04:13:51+00:00

I need to send a txt file via socket connection when requested by client.

  • 0

I need to send a txt file via socket connection when requested by client. The problem is I can’t figure how to convert from binary format to ASCII. With this code the client receive only junk characters.

ifstream file;
char output [logfilesize];

file.open( "log.txt" );

file >> output;
send(socket, output, sizeof(output), 0);
cout << output << endl;

file.close

Sorry if the question have been asked multiple time but after reading all post I could not figure how to make it work.

Thank’s!


Edit: Working code!

int lenght;
char * logf;
ifstream file;

file.open("log.txt");   
file.seekg(0, ios::end);
lenght = file.tellg();
file.seekg(0, ios::beg);
logf = new char [lenght];
file.read(logf, lenght);
file.close();

cout.write (logf, lenght);
send(soc,logf , lenght, 0);
delete[] logf;
  • 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-31T04:13:53+00:00Added an answer on May 31, 2026 at 4:13 am

    This sizeof(output) will return the size of the original array you declared rather than the number of characters you read in via the stream operator. You can only use the stream in operator from the file stream if you know the file is text (i.e. you want to read a single delimited string). Else you should read the required number of bytes from the stream via the read() calls. If it is a string, then use the strlen() function to determine the length of the string (NOTE: this will not work for binary data).

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

Sidebar

Related Questions

i need to read a txt file from clients and then send it to
I need to send some information from the command prompt to a text file
I need to read form TXT file on the server side and send its
Holy Dudes from stackoverflow, MY PROBLEM: I need to send an email with 3
In a server / client environment, I need to send a plain text file
All,In my requirement need to send a mail with attached text file, which is
I'm trying to send a file between a client and a server in my
I need to upload a file in GWT in the background, ie. not from
Effectively I'm trying to send some template emails so that I can test a
Currently I use this code to read a txt file with words and perform

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.