-5

i am trying to write a small automated program to calculate some values for me and output some text to a simple .txt file. do the redirection symbols < > & << >> work the same in C++ as they do in the command line for batch scripts? When i try to search how to redirect to a .txt file in C++. All of the examples, and tutorials i have found are presented in a manner that assumes IO on the console like the following.

    cout::<<"show this text on the console";
    cin::>> whatever you would call here to accept user input.

what i want to know is will it work to do it this way?

    #include <string>
     using namespace std;

    int main()
    {
        int X = 0;
        string zero = "touchPress 0 483 652\n";
    if {
     (X=0)
    zero>>C:\test.txt;
    x+5;
   } return 0;

}

Casey French
  • 139
  • 2
  • 5
  • 14
  • 6
    No offense but there are quit a few basic mistakes in this code. You would do well to get a C++ book or a lengthy tutorial on the subject before continuing. – Borgleader Dec 25 '15 at 00:19
  • 2
    None of your code compiles. I suggest you get a good book and work through that methodically. Recommended books are: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Dec 25 '15 at 00:20
  • Here is a basic tutorial about [C++ Files and Streams](http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm). I think it answers your question. – zvone Dec 25 '15 at 00:35
  • For what it's worth: Even though the C++ `>>` and `<<` operators may look similar to the ones in the console, they have subtly yet fundamentally different semantics. In C++ it is misleading to think in terms of "redirection" here; rather, you should think in terms of working with *streams*. zvone already posted a really good tutorial, you should check it out to understand streams (but solid language basics are required before tackling that topic!) – chris Dec 25 '15 at 00:57
  • 1
    @chris "subtly yet fundamentally different semantics" - there's nothing subtle about it. They are completely dissimilar and unrelated. There's no stream redirection at all. – sehe Dec 25 '15 at 01:01
  • @sehe That's just a matter of perspective. On a technical level I think we are in agreement, apart from picking words ;-) – chris Dec 25 '15 at 01:07
  • @chris Practically, though, the only useful level to learn a language (c++ in particular) is _at the technical level_. The sooner the OP fixes his/her "perspective", the better. I don't think singing along is doing the OP a service. (That's not picking words. It's picking objectives) – sehe Dec 25 '15 at 01:10

2 Answers2

2

Your code does not work. I am not absolutely sure what is the desired behaviour, but this code writes the string zero to a file:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() 
{
    int X = 0;
    string zero = "touchPress 0 483 652\n";
    ofstream myFile("C:\\Data\\test.txt");
    //a condition which is always true
    if (X==0)
    {
      myFile<<zero;
      X + 5; //this is valid but useless
    }
    return 0;
}
Andrea Dusza
  • 2,080
  • 3
  • 18
  • 28
  • 3
    How on earth would you figure that was the intended meaning :) `(X==0)` is always true. Hmm. Also not sure this is doing the OP a service, since obviously having more code to copy/paste is not the right next step for him/her to learn – sehe Dec 25 '15 at 00:27
  • :) It makes more sense than `x+5;` ! OP was probably just trying out some basic keywords, etc. – Andrea Dusza Dec 25 '15 at 00:28
  • I can't refute that. Splendid argument :) – sehe Dec 25 '15 at 00:28
  • 1
    At least it compiles and does what was asked (writing text to a file) :-) – chris Dec 25 '15 at 00:29
  • i do see looking at my question and the code i have posted that it doesnt make it completely clear what the intention of the code is, i did not post all of the code i will need, because its a minimum of 240 if loops the X +5; line of code is for concurrency. Any suggestion on how i can make the question more valid is welcomed. – Casey French Dec 25 '15 at 00:50
  • 1
    @CaseyFrench The only problem is that your code contains more errors than logic, which makes debugging really hard. Next time, you should try to compile your code before posting it. Your question is otherwise valid. Continue learning, reading books, doing tutorials and achieve smaller goals first before writing a program with 240 if loops. – Andrea Dusza Dec 25 '15 at 01:11
  • @Andrea Dusza I am working on getting a compiler running on my laptop right now. Unfortunately the only way I know of to compile for now is on my Android phone, which complicates the issue of testing my code because in order to write to a file I will have to write a complete file path to the SD card as creating a file on the root of the system isn't permitted because the compiler is not a root application, it is also missing a few libraries like the fstream, so there isn't alot of actual programming I can attempt that won't throw an error in the minimalist IDE available. – Casey French Dec 25 '15 at 02:02
  • fstream is a core part of the standard library. Any reasonable C++ dev tools distribution includes it. Getting a compiler to run is really easy these days. On Windows, use the MinGW installer to install the "g++" package, and it will do the rest. As to the permissions problem: You are supposed to do that kind of work under C:\Users\{username} (or /home/{username} on Linux). – chris Dec 25 '15 at 11:24
  • @chris While android is linux based, it does not have multiple user profiles in most production builds, home username does not exist, the closest would be /tpm/data/local. compiling on android is specifically in an application written, it is not done in the shell, its possible if you call the activity manger of android, but a complete waste of time and quite possible that it will not work at all seeing as how calling the application in the activity manager is still going to launch the application, instead of its compiler, and output log. – Casey French Dec 28 '15 at 05:30
1
    #include <fstream>
    int main(){
        string zero = "touchPress 0 483 652\n";
        std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
        fout << zero; //same as cout << zero;//but in the file
        return 0;
    } 

fout as cout, i just reworked your barely alive program. is this what you wanted?

I suck
  • 17
  • 3
  • this will answer my question as well, i did not want to post the enormous amount of code its going to require to finish this program, clearly i should have posted more for the scope of my intention to be clearer to others reading and searching for the same solution. – Casey French Dec 25 '15 at 00:54
  • edited my post to answer a bit better – I suck Dec 25 '15 at 01:04
  • 2
    Line 4 of your example will *not* create the file in the folder where the `.exe` is, it'll create it in the current working directory (iff you have `w` perms there) – набиячлэвэли Dec 25 '15 at 01:07