![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0
![]() |
C++ examples
Hi!
I have used VB6 for years and resently moved to Gambas (linux-version of VB), and I have some trouble too learn C++, why isn't there any simple examples on this in linux,, ie. "man fopen" does not give me any examples :mad: (I really liked the old QBasic help system, is there some for C++?)In VB6 I could write a file with doing follow: string myfile;
string temp;
open myfile on #1 for input // something like that... its a while since I have used VB.
while not eof(1)
(
input line #1, temp
print temp;
)
close #1how is this done in C++ (G++ actually), to read line by line, and print it to the screen. I have managed to do this: #include <iostream>
#include <fstream>
using namespace std;
int main()
{
char string1[20], string2[20], string3[30];
ifstream fin("file.dat");
if (!fin.is_open()) {
cerr << "Error: Could not open file." << endl;
exit(1);
}
fin >> string1;
fin >> string2;
fin >> string3;
cout << string1 << " " << string2 << " " << string3 << endl;
fin.close();
return 0;
}And still I got alot of other questions, but those I'll take later. plz, forgive me if there some english errors.. ![]() |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Google. You'll be inundated. You can also search THIS forum for links. You may not find anything specific to fopen, but you'll find plenty of links to good tutorials. Loops are control constructs; one places them wherever one needs repetetive operations. They aren't restricted to file operations, but come in handy there, as well. There are many input and output functions over and above the insertion and extraction operators (<< and >>). Why? Because they don't serve all purposes well. Pick and choose, just as you do on the candy aisle (or, at least, I do). Read the documentation for what you pick. Find how it informs you of failure and be sure to ask it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Did you take a look at this:
http://www.cs.odu.edu/~jbollen/CS149.../cpp_files.pdf This is quite informative about fin and fout.
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
fopen is C. Using the ifstream constructor is C++.
Do you want something like: #include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string string1, string2, string3;
ifstream fin("file.dat");
if (!fin.is_open())
{
cerr << "Error: Could not open file." << endl;
exit(1);
}
getline(fin, string1);
getline(fin, string2);
getline(fin, string3);
cout << string1 << endl << string2 << endl << string3 << endl;
fin.close();
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0
![]() |
Wow, so many responses in soo little time... (I'll stick to this forum from now on)
Thanks nnxion, getline is what I have been searching for along time. Now I need to figure out how to read through any file, since I dont know how to detect the EOF. ( The final idea is to read the entire file in to a string[] , so I can make a very simple editor) Jimbo: C++ is the language, g++ is a compiler. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
READ your documentation. If you don't know how to detect EOF, it's clear you haven't.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
Try to do the best you can and try examples out on your compiler, learn to use the debugger, he's among my best friends when it comes to programming. (That's just an expression, if you're worried )If you're still stuck at that point, then come to us and ask us for help.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|