Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ extracting data to txt or

  1. Mar 14, 2015 #1

    ChrisVer

    User Avatar
    Gold Member

    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:
    Is it possible to write in C a program of the form:

    and let it somehow extract each k[j] into the text?
     
    Last edited: Mar 14, 2015
  2. jcsd
  3. Mar 14, 2015 #2

    DrClaude

    User Avatar

    Staff: Mentor

  4. Mar 14, 2015 #3

    ChrisVer

    User Avatar
    Gold Member

  5. Mar 14, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    You have to open an fstream for output. The second link I gave will explain that.
     
  6. Mar 14, 2015 #5

    ChrisVer

    User Avatar
    Gold Member

    yes I made it work :) Thanks!
     
  7. Mar 14, 2015 #6

    ChrisVer

    User Avatar
    Gold Member

    I did write this:
    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?
     
  8. Mar 14, 2015 #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.
     
  9. Mar 14, 2015 #8

    ChrisVer

    User Avatar
    Gold Member

    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!
     
  10. Mar 14, 2015 #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.
     
  11. Mar 14, 2015 #10

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  12. Mar 14, 2015 #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:

    Code (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: Mar 14, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ extracting data to txt or
Loading...