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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:35:17+00:00 2026-06-07T13:35:17+00:00

this is my question. i opened a .jpg image and wrote its each byte

  • 0

this is my question. i opened a .jpg image and wrote its each byte in a .txt file seperated with a comma. it was success. now i want to use that txt file to rebuild the image. the img.txt looks something like
255,216,255,224,0,16,74,70,73,70,0,1,1…….
the following code created the image.jpg, with the size if the original image, but but the image is not visible. im expecting help from somebody…

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<cstdlib>
using namespace std;

int main(){

    char *s;
    long x;
    ifstream is("D:\\test\\img.txt");
    is.seekg(0,ios::end);
    x=is.tellg();
    is.seekg(0,ios::beg);

    s=new char[x];
    is.read(s,x);
    is.close();

    stringstream str;
    char a[4];
    int y = 0;
    for(int i=0; i<=x; i++) {
        if (s[i] != ',') {
            a[y] = s[i];
            y = y + 1;
        }
        if (s[i] == ',') {
            str << (unsigned char)atoi(a);
            a[0] = '\0';
            a[1] = '\0';
            a[2] = '\0';
            a[3] = '\0';
            y = 0;
        }
    }

    const char *ss=(str.str()).c_str();
    ofstream ex("D:\\test\\test.txt");
    ex << ss;

    ofstream fileo("D:\\test\\image.jpg",ios::binary);
    fileo.write(ss,(str.str()).length());
}
  • 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-07T13:35:19+00:00Added an answer on June 7, 2026 at 1:35 pm

    Your code as written worked for me with Visual Studio 10 SP1. However, there is a subtle bug depending on your STL implementation (and luck):

    Your code:

    const char *ss=(str.str()).c_str();
    

    Is using a temporary that has gone out of scope. What ss points to could well be garbage immediately (or any time in the future) after this line executes. The reason is std::stringstream::str() returns a copy of the string, it’s safe to call std::string::c_str() on this copy, but that pointer will not be valid once the original (temporary) goes out of scope.

    To fix this, make sure you copy the string out of the stringstream object so that the lifetime is known, like this:

    std::string contents = str.str();
    ofstream ex("D:\\Profile2.jpg.txt");
    ex<<contents;
    
    ofstream fileo("D:\\Profile2.jpg",ios::binary);
    fileo.write(contents.c_str(), contents.length());
    

    To reiterate, both versions are working for me, but the version I propose is actually working by design, as opposed to luck.

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

Sidebar

Related Questions

This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question appeared when I recently opened a rather old driver for my raid
In reference to this question about read() and write(), I'm wondering if each open
Guys, I opened this question (I hope could help some others) but reading stackoverflow
OK, so I opened up this question yesterday and got an answer fairly quickly.
Started with this question: OpenID. How do you logout OK. So OpenID does not
Referencing Is Facebook an OpenID provider? here. This is kind of an additional question
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a
This question is really basic. What is the performance difference between removing a UIView

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.