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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:38:27+00:00 2026-06-09T19:38:27+00:00

I have two programs working together to write and read data from a file.

  • 0

I have two programs working together to write and read data from a file.

The first program is a C# Form. It takes in a file name from the user and writes it out to a file on the C drive and exits/closes the C# application.

Revelvant code:

private void startBtn_Click(object sender, System.EventArgs e)
{
     using (StreamWriter writer = new StreamWriter("C:\\File.txt"))
     {
         writer.WriteLine(fileName.Text + ".txt");
         writer.Close();
     }
     Application.Exit();
}

The second program in C++. This program creates a C# application only when the f10 is pressed and then halts until the C# application closes. Then the C++ program resumes and reads in the file name that was wrote to the file on the C drive. The C++ program is highly specialized/customized and additions to it have to be handled carefully.

Relevant Code:

std::string path("C:\\CSharpApp.exe");
system(path.c_str()); //Halts here until CSharpApp exits
if(!reader.is_open()) //Reader is a global ifstream object
{
    reader.open("C:\\File.txt");
}
char temp[MAX_FILE_NAME];//250
reader >> temp;
reader.close();
string temp2 = temp;
//cout << temp2 << endl; //Using to debug
FileName = "C:\\data\\" + temp2;//temp2 contains a file name ending with .txt
if(!writer.is_open())//Writer is a global ofstream object
{
    writer.open(FileName.c_str(),ios::app);
}
//write some stuff to file
writer.close();

Pressing the f10 key the first time invokes the C++ code and it calls the C# app and the name of the file is stored correctly. The C++ program resumes when the CSharpApp closes and the file name is read correctly.

Problem:

Any additional f10 key presses result in the cout statement showing that temp2 has a value of nothing, I just see the endl in the console. The File.txt contains the correct name of the next file but for some reason the C++ program only gets the file name correctly the first time f10 is pressed.

The purpose of this functionality is to press f10 and create a new file with a new name each time f10 is pressed.

I don’t think both programs can access the file at the same time because the execution of the C# app halts the C++ program. Also I tried changing the writer inside the CSharp application to append the filenames rather then overwrite the file. This caused the C++ application to print out temp2 as being the first file name only, and no blank lines. Not sure what this means.

I am currently working to solve this and will update as I make progress.

UPDATE:

I added the following in place of: reader >> temp;

Code:

if(reader >> temp)
    cout << "Read in ok" << endl;
else
    cout << "PROBLEM OCCURRED" << endl;

Now my program is functioning correctly, however I am not clear as to why it is working now. To me I’m assuming that adding this if test and cout statements must have given the program more time.

Thanks for the help

  • 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-09T19:38:29+00:00Added an answer on June 9, 2026 at 7:38 pm

    I am suspicious that the file write in the C# app is not finished committing to disk before your C++ app is trying to read it. You might try adding writer.Flush() before the call to writer.Close() and see if it makes any difference.

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

Sidebar

Related Questions

i have two programs working on the same file i want the first program
I have two (UNIX) programs A and B that read and write from stdin/stdout.
In Windows, I currently have two programs working as follow. Program A calls Program
I have two programs, the first is a producer: public class Producer { public
I have two programs, Writer and Reader. I have a FIFO from Writer to
I have two programs: one CLI program, and one GUI. The GUI is a
I've got a table recording views of programs. Each program can have two different
I am working on a program using python, HTML and javascript. I have two
I'm working in linux. I have two programs that run for infinite time (
I am working on a program where I have divided objects in to two

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.