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

  • Home
  • SEARCH
  • 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 1047887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:22:16+00:00 2026-05-16T16:22:16+00:00

I am having problems with the code bellow. It can write fine if i

  • 0

I am having problems with the code bellow.

It can write fine if i kill the read section.
It can read fine if i kill the write section and the file has already been written.
The 2 don’t like each other. It is like the write stream is not closed… though it should be.

What is wrong?

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace System;

int main(array<System::String ^> ^args)
{
 string a[5];
 a[0]= "this is a sentence.";
 a[1]= "that";
 a[2]= "here";
 a[3]= "there";
 a[4]= "why";
 a[5]= "who";

 //Write some stuff to a file
 ofstream outFile("data.txt");     //create out stream
 if (outFile.is_open()){       //ensure stream exists
  for (int i=0; i< 5; i++){
   if(! (outFile << a[i] << endl)){    //write row of stuff
    cout << "Error durring writting line" << endl; //ensure write was successfull
    exit(1);
   }
  }
  outFile.close();
 }

 //Read the stuff back from the file
 if(!outFile.is_open()){  //Only READ if WRITE STREAM was closed.
  string sTemp;   //temporary string buffer
  int j=0;    //count number of lines in txt file

  ifstream inFile("data.txt");  
  if (inFile.is_open()){
   while (!inFile.eof()){
    if(! (getline(inFile, sTemp)) ){    //read line into garbage variable, and ensure read of line was
     if(!inFile.eof()){       //successfull accounting for eof fail condition.
      cout << "Error durring reading line" << endl; 
      exit(1);
     }
    }
    cout << sTemp << endl;  //display line on monitor.
    j++;      
   }
   inFile.close();
  }
  cout << (j -1) << endl;  //Print number lines, minus eof line.
 }

 return 0;
}
  • 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-16T16:22:17+00:00Added an answer on May 16, 2026 at 4:22 pm

    You have a memory overwrite.

    You have six strings, but dimension only for five strings

     string a[5];
     a[0]= "this is a sentence.";
     a[1]= "that";
     a[2]= "here";
     a[3]= "there";
     a[4]= "why";
     a[5]= "who";
    

    this can cause the rest of your program have unexpected behavior.

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

Sidebar

Ask A Question

Stats

  • Questions 544k
  • Answers 544k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is known as Balance puzzle. See Marcel Kołodziejczyk’s Two-pan… May 17, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer Yes, the MS implementation of anonymous methods effectively creates one… May 17, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer I should say that skinning in Spark framework is a… May 17, 2026 at 7:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am having problems with my JScript code. I am trying to loop through
I'm having some problems with the following code: private class ClientPluginLoader : MarshalByRefObject {
I am currently having some problems with <li> in IE6 essentially my code looks
I'm having a problem with my Seam code and I can't seem to figure
Having a problem getting a TreeView control to display node images. The code below
I am trying to convert this test code to C# and having a problem
I am having some trouble with this code . The problem is when i
Im having problems displaying records to my view when passing viewdata to a user
Im having problems building a query with the linq to sql data query expression
I have recently started having problems with TortoiseCVS, or more specifically with plink, 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.