Can C++ Write Data to a TXT File?

  • Context: C/C++ 
  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    C++ Data
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
10 replies · 2K views
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:
Physics news on Phys.org
You have to open an fstream for output. The second link I gave will explain that.
 
  • Like
Likes   Reactions: ChrisVer
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
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.
 
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: