C++ extracting data to txt or

  • C/C++
  • Thread starter ChrisVer
  • Start date
  • Tags
    C++ Data
In summary: This will allocate the array dynamically in memory.In summary, the program can't calculate data and return them into
  • #1
ChrisVer
Gold Member
3,378
464
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
  • #3
  • #4
You have to open an fstream for output. The second link I gave will explain that.
 
  • Like
Likes ChrisVer
  • #5
yes I made it work :) Thanks!
 
  • #6
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?
 
  • #7
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.
 
  • #8
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!
 
  • #9
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 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:

Related to C++ extracting data to txt or

What is C++ extracting data to txt?

C++ extracting data to txt refers to the process of using the C++ programming language to retrieve data from a source and saving it in a text file (.txt) format.

How is C++ used for extracting data to txt?

C++ can be used for extracting data to txt by using file input/output (I/O) functions such as ofstream, ifstream, and fstream. These functions allow the programmer to open, read, and write to text files.

What are the benefits of using C++ for extracting data to txt?

C++ is a powerful and efficient programming language, making it a great choice for extracting data to txt. It also allows for low-level memory management, which can be useful when dealing with large amounts of data. Additionally, C++ has a wide range of libraries and tools that can aid in the extraction and manipulation of data.

Can C++ extract data from different types of sources?

Yes, C++ can extract data from various sources such as databases, websites, and other programs. As long as the data is accessible, C++ can be used to extract it and save it in a text file.

Are there any limitations to extracting data to txt using C++?

One limitation of using C++ for extracting data to txt is that it requires knowledge of the language and its syntax. This may make it more challenging for beginners to use compared to other languages. Additionally, C++ may not be the best choice for extracting data from sources with complex structures or formats.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
691
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
6
Views
920
Back
Top