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

Related Questions

I've been having problems with this code I had spent the last 3 hours
I'm having some problems trying to get the code below to output the data
I'm having problems with some code to create a CDialog based window. The code
I am having problems with this code: <?php $new_value = 'testing'; $con = mysql_connect(localhost,user,pass);
I'm having problems the following code gives me no results. however if I uncomment
Basically i am having problems with my code - this is homework so would
I'm having problems with the following code (I am a beginner in most things
Im trying to read in image file from a server , with the code
I am new to node.js. I am having a few problems in the code
I seem to be having a problem. I want to write some code that

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.