Can C++ Write Data to a TXT File?

  • Context: C/C++ 
  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    C++ Data
Click For Summary

Discussion Overview

The discussion revolves around the ability of a C++ program to write data to a text file. Participants explore how to implement file output in C++, share code snippets, and address issues related to array bounds and program crashes.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about writing calculated data to a text file using C++.
  • Several participants note that the question is basic and suggest resources for learning about file I/O in C++.
  • A participant shares a working code snippet that successfully writes data to a text file but encounters a crash when the loop variable exceeds a certain limit.
  • Another participant points out that the crash is likely due to exceeding the bounds of the predefined array size.
  • There is a discussion about the role of the optimizer in the toolchain and whether it could affect the program's behavior when accessing memory.
  • One participant expresses uncertainty about the operating system's memory management and suggests a revised code example that avoids hardcoding array sizes.

Areas of Agreement / Disagreement

Participants generally agree that exceeding array bounds can lead to crashes, but there is disagreement regarding the role of the optimizer and how memory management is handled by the operating system.

Contextual Notes

Participants discuss limitations related to array size and memory access, highlighting the need for careful management of array indices to prevent out-of-bounds errors.

ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
Hi, is there any way to let my C++ program calculate some data and return them as results into a txt file?

For example let's say I want to make a txt with the following things inside:
1
2
3
4
5

Is it possible to write in C a program of the form:

#include <iostream>
#include <cmath>
#include<vector>

using namespace std;

int main(){
int k[5];
k[0]=1;
for (int j=0; j<5; j++)
{
k[j+1]=k[j]+1;
}
return 0;
}

and let it somehow extract each k[j] into the text?
 
Last edited:
Technology news on Phys.org
You have to open an fstream for output. The second link I gave will explain that.
 
  • Like
Likes   Reactions: ChrisVer
yes I made it work :) Thanks!
 
I did write this:
#include <iostream>
#include <fstream>

using namespace std;

int main(){

int k[5];
ofstream test;
test.open ("test.txt");

k[0]=1;
for(int j=0;j<10;j++){
k[j+1]=k[j]+1;
test<< k[j] <<"\n";
}
test.close();return 0;
}

And it works as I'd like for it to... But unfortunately for j_max>=15 the program crushes...
Do you have some feedback? Or is it my machine's problem?
 
I am not sure what you mean with "j_max >= 15" but what may cause your crash is in the code you posted. In the program your for-loop variable "j" will exceed the pre-defined size of array "k".
This will give an array out of bounds error.
 
JhpMeer said:
I am not sure what you mean with "j_max >= 15" but what may cause your crash is in the code you posted. In the program your for-loop variable "j" will exceed the pre-defined size of array "k".
This will give an array out of bounds error.

Oh my god... I think you are right...
Well that is even worse, because when I put j<14 it was able to reproduce:
1
2
...
14

While when I put j<15 (or more) it crashed.

o0) it's weird, now that I think about it, that it worked!
 
When you convert your code to a program(machine language) your code goes through a "tool chain". In this tool chain there is the "optimizer". It looks at your code and changes it if it finds something that could pose a threat in your program. Perhaps the optimizer changed it so it would work up until j = 15. But not beyond that.
 
  • Like
Likes   Reactions: ChrisVer
  • #10
JhpMeer said:
When you convert your code to a program(machine language) your code goes through a "tool chain". In this tool chain there is the "optimizer". It looks at your code and changes it if it finds something that could pose a threat in your program. Perhaps the optimizer changed it so it would work up until j = 15. But not beyond that.
I don't think the optimizer can do such a thing.

So long as a program is addressing the memory it was allocated, everything is fine from the operating system's point of view. It is only when the program tries to access memory that doesn't belong to it that you get a segmentation fault. The latter situation is actually better, because it is then obvious that there is a bug in the code. Otherwise, it can take a long time to figure out that some variables we modified because a pointer was pointing at the wrong place.
 
  • #11
I don't know if that is true or not. I am not sure how an OS does memory management of arrays. Anyway, the optimizers can do a lot of scary things. Best practice to avoid this problem:

C:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

int array_limit = 10;
int array[array_limit];
ofstream test;

test.open("test.txt");

for(int index = 0; index < array_limit; index++){
array[index] = index + 1;
test<<array[index]<<'\n';
}

test.close();

return 0;
}

You can use a variable like in the example above or even use a define.
 
Last edited:

Similar threads

Replies
12
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K