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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:19:43+00:00 2026-05-27T11:19:43+00:00

Im working on a text file decoder along with an encoder, they work off

  • 0

Im working on a text file decoder along with an encoder, they work off of two different text files. The decoder prints the decoded message underneath the encoded message but it also prints a bunch of other stuff as well. How do i fix this

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

int main() {
  ifstream fin; // input file
  string line;
  ofstream fout;

  //open output file
  fout.open("secret.txt", ios::app);
  if (!fout.good()) throw "I/O error";

  // open input file
  fin.open("secret.txt");
  if (!fin.good()) throw "I/O error";

  // read input file, decode, display to console
  while (fin.good()) {
    getline(fin, line);

    for (int i = 0; i < line.length(); i++) // for each char in the string...
      line[i]--; // bump the ASCII code down by 1

    fout << line << endl; // display on screen
  }

  // close file
  fin.close();

  return 0;
}

the text file from the encoder reads

Uftujoh234

Ifmmp!nz!obnf!jt!cpc

Dmptfe!

Uftujoh

which decodes to

Testing123

Hello my name is bob

Closed

Testing

this is all the extra stuff it also prints in the text file

Sdrshmf012
Gdkknlxm`ldhrana
Bknrdc
Sdrshmf
Rcqrgle/01
Fcjjmkwl_kcgq`m`
Ajmqcb
Rcqrgle
Qbpqfkd./0
Ebiiljvk^jbfp_l_
@ilpba
Qbpqfkd
Paopejc-./
Dahhkiuj]iaeo^k^
?hkoa`
Paopejc
O`nodib,-.
C`ggjhti\h`dn]j]
>gjn`_
O`nodib
N_mncha+,-
B_ffigsh[g_cm\i\
=fim_^
N_mncha
M^lmbg`*+,
A^eeh
  • 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-27T11:19:44+00:00Added an answer on May 27, 2026 at 11:19 am

    The extra data you see is actually valid output from decoding the data in "secret.txt".

    I’m not sure if this is what you want, but are you aware that you are reading and writing to the same file each time you run your application?

    You’ll append more and more “decoded” data to the file, and therefore you get the extra output you are referring to.


    Also, there is an issue with your while-loop.

    fin.good () will remain true until some of the error bits has been set inside of fin, though it will enter the loop one time too much since you should check to state of the stream immediately after your call to getline (fin, ...).

    Currently the reading will fail but you will still process the “unread” data.


    std::getline will return the stream object, and since a std::istream (as well as std::ostream) implicitly can be converted to a boolean to check it’s current state you should use that as your loop-condition.

    Change your loop into something as the below and see if that solves your problem.

      while (getline (fin, line))
      {
        for (int i = 0; i < line.length(); i++) // for each char in the string...
          line[i]--; // bump the ASCII code down by 1
    
        fout << line << endl; // display on screen
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with text files. Actually one file, where I have to copy information
I'm working in Visual Studio 2005 and have added a text file that needs
I'm working on some doc file, that when copied and pasted into a text
I'm working with a very big text file (755Mb). I need to sort the
I'm working in ASP.NET application and reading a pipe delimited text file. After reading
I am trying to read a text file using fscanf.I am working in eclipse
I'm working with C# and MVC3 and I'm trying to upload a text file
I am working on a project to parse out a text file. The file
I am working on a project that parses an incoming text file. I am
i am working on a program that reads in a text file that the

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.