Very Simple Noob Request [C++]

Posts 1–15 of 19 · Page 1 of 2
Very Simple Noob Request [C++]
Well I am working on C++, but I just want to know how..to read text from a file and display that text in a messagebox.
Search on MSDN/Google for Input/Output Streams ; They are the ones that will allow you to read from files =).

Saying more would kill all the fun.

Enjoy.

For The Display part, Once your String is buffered, It's pretty easy too, Expecially if you use MiCrapSoft .Net
Lol the fun is already killed when your computer crashes everytime you think the code might work.
Hm.

How could you possibly have such a Memory Leak with "Safe" allocations... Meah~

Well, You found your way around with these Streams ? Or you need an more concrete Example ?.
btw I searched and got this

Code:
  #include<fstream.h>
    int main()
    {
        char str[2000];
        fstream file_op("c:\\test_file.txt",ios::in);
        while(file_op >> str)
        cout << str ;
 
        file_op.close();

        return 0;
    }
what would I use to save it to a variable..or is "str" the variable I would use??
If you want to read only from file, You are better to do an ifstream instead of an fstream.

Yaps, str will be your string ( Well.. Char[2000] ), And the cout line will print the result to console.

( Pheraps You should do Basic C++ Tutorials/Courses/Books =), Could teach you a lot of basis, And Streams are in these Basis before attempting stuff. =) )
Im very confused I have the includes in place, and I am getting all these errors...I am testing in a windows form first...and when I get the code to work I want to move it to a Dll Project..;s The funny thing is I code Java, and Grand Theft Auto Private...which is bassed of C++...and I code that just fine this should not be so confusing..but it is..

My Code

Code:
ifstream f("test.txt");
f>>n;
f.close();
cout<<n;
cin.get();
Errors

Code:
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(30) : error C2065: 'ifstream' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(30) : error C2146: syntax error : missing ';' before identifier 'f'
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(30) : error C3861: 'f': identifier not found
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(31) : error C2065: 'f' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(31) : error C2065: 'n' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(32) : error C2065: 'f' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(32) : error C2228: left of '.close' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(33) : error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(33) : error C2065: 'n' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(34) : error C2065: 'cin' : undeclared identifier
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(34) : error C2228: left of '.get' must have class/struct/union
1>        type is ''unknown-type''
Oh.

Bad Start.

Windows Forms Library use .Net/Managed C++

The code you have is Native C++.

So, Two things ; Porting your code back and forth from Native to Managed C++ isn't easy for someone that beggins coding, And These do not use all the same methods anyways.

So, You should start directly with your DLL, And it should not cry too much, But as I said previously ; Are you sure this is the easiest thing to begin with ?
Lol Yes...I need to do something else with it..that is to confusing to explain....so I asked for a read from file to messagebox..for I can mess around with the code and make it to something different..so if you can spoon feed me that code that would work for a .dll that would be extremely awesome of you.
I could post right below the code you want but you wouldn't have learned a thing.

Are you sure you don't want to start easily ?

Anyways, For messageboxs, You need the "stdio" header, The code is MessageBox/MessageBoxA,

And you are alright with "your" code to get the string from file, At one exception; You need to use the namespace std for this to work ( using namespace std; ) Below your includes and stuff, And cut off the cout part, As this is CLI Code.

But as I said, This taught you nothing.
it taught me alot, I learn by analyzing codes giving to me, and re looking them and changing them to different things.


ok I updated my code


Code:
ifstream f("test.txt");
f>>n;
f.close();
cin.get();
now i get these errors

Code:
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(34) : error C2061: syntax error : identifier 'n'
1>c:\users\michael\documents\visual studio 2008\projects\combat arm console\combat arm console\combat arm console.cpp(36) : error C2065: 'n' : undeclared identifier
I'm guessing I have to define N as a string? now how would I do that?

update..i guessing to use

char n to define n as a string
and if thats right my last error is

Code:
syntax error : missing ';' before identifier 'ifstream'
Declare your "n", Lawl.

If you follow your example ;

char n[2000];

Edit -->

You forgot the ";" at the end of your declaration.

need to learn Basics. Again <3
no i had the ; at the end the reason i got the error is cause the string was un defined and when ifstream was spaced next to f(""); with out a ; it was a undefined identifier
Why not try doing wat u said.
it taught me alot, I learn by analyzing codes giving to me, and re looking them and changing them to different things.
except instead of changing things randomly take the time to learn why it doesn't work. Or better yet learn to read error messages.

wth am I up at 6:40 am, i dont fkin know.... =/
String is not an Native C++ term.

I Say string, But It's char*, Too used to Managed C++, Sowwies.

@Why
Lawl, Well, 7:41 Here, Guess I should get some sleep, I woke up..... ~50 Hours ago ?
Posts 1–15 of 19 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us