I was just wondering what the "endl" is for. I saw it in the tutorials but don't know why it was there.Code:cout<<"Time is "<<ctime(&date)<<endl;
Thanks!
I was just wondering what the "endl" is for. I saw it in the tutorials but don't know why it was there.Code:cout<<"Time is "<<ctime(&date)<<endl;
Thanks!
It is there to end the line. (endl stands for end_line). So the next time you use cout for something it is printed on the next line...
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
so that is another way to code?Code:cout<<"Time is "<<ctime(&date)<<"/n";
Is the "endl" the proper way to code it?
sorry getting off topic...
Thanks, I revised my code. =)
I have been going through the tutorials and don't have much questions (actually I do, but I think they'll be answeared later, and I like finding out on my own)
with tthe code entered before, can someone tell me how to get more words out the example.txt, and explain where to put it and why it's done that way?
the new code
Code:#include <fstream> #include <iostream> #include <stdio.h> #include <time.h> using namespace std; int main() { int thisisanumber; char str[50]; //Creates an instance of ofstream, and opens example.txt ofstream a_file ( "example.txt", ios::app ); // Outputs to example.txt through a_file // a_file<<"This text will now be inside of example.txt \n"; // keeps these as example of writing to a txt through a_file //Opens for reading the file ifstream b_file ( "example.txt" ); //Reads one string from the file b_file.read(str, 50); str[50]='\0'; // b_file>> str; <replace this if above does not work //Should output 'this' cout<< str <<"\n"; a_file<< str <<"\n"; // b_file is closed implicitly here time_t date=time(NULL); cout<<"Time is "<<ctime(&date)<<endl; cout<<"Please enter a number: "; cin>> thisisanumber; cin.ignore(); cout<<"You entered: "<< thisisanumber <<"\n"; a_file<<"You entered: "<< thisisanumber <<"\n"; a_file<<"Time is "<<ctime(&date)<<endl; cin.get (); // Close the file stream explicitly a_file.close(); return 1; }
Last edited by gothpunk; 06-20-2005 at 04:15 AM.
Ok, here we go... In your example, you just get 10 characters out of the example.txt.
Output:Code:#include <fstream> #include <iostream> using namespace std; int main() { //declare our variable, the string we will later read from our example.txt char str[1025]; //open example.txt for output ofstream a_file ( "example.txt"); //putting some strange stuff into the file a_file<<"This text will now be inside of example.txt"; a_file<<" here is a number, too: 1234567890!!"<<endl; a_file<<" and something more here"; a_file<<endl<<"maybe something strange here too"; //closing the output file a_file.close(); //opening the input file ifstream b_file ( "example.txt" ); //reading from the input file //readsome function must get two values from us: //the first one - a pointer to our string (str); //and the second how many letters we want to read. //We really don't know how many letters there are //in this file, so we guess a large value //that is larger than the real contents, so that //we are sure that the whole string fits into //our variable. //readsome function returns how many letters it //actually had read. int bytes_read=b_file.readsome(str, 1024); //printing some debug info: cout<<"Have read "<<bytes_read<<" letters from example.txt"<<endl; //terminating the string with \0 symbol, so that //we know where it ends str[bytes_read]='\0'; //printing our results on the screen cout<<"The contents of example.txt:"<<endl; cout<<"----"<<endl; cout<<str<<endl; cout<<"----"<<endl; //closing the input file b_file.close(); return 0; }
Code:Have read 137 letters from example.txt The contents of example.txt: ---- This text will now be inside of example.txt here is a number, too: 1234567890!! and something more here maybe something strange here too ----
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
mathfan, that's a lot cleaner code!
there's some stuff im not familiar with though can you go in to more detail?
so will this code snippet read an infinite amount from example.txt?Code:int bytes_read=b_file.readsome(str, 1024);
int is saying that we are looking for a number ?right?
bytes_read <<---- what is this? does this ask example.txt how big it is?
= this compares bytes_read to b_file.readsome?
readsome I don't understand this
(str,1024) is this telling b_file.readsome to go read up to this many charactors?
Function readsome() has the following syntax:
readsome(the_string_you_want_to_read_to, how_many_letters_do_you_want_to_read)
if you want to read 10 letters, your code is:
if you want to read 100 letters, your code is:Code:readsome(str, 10);
Now a bit about the int bytes_read...Code:readsome(str, 100);
I understand that it is a bit confusing all together. I don't know if
you have learnt about functions, and if not, it's even more confusing
I guess. But let me try to explain it.
When we call a function, it can return a value (but doesn't need to). In
our case, readsome() does return a value. It returns how many letters it
actually has read. You wonder probably why is that needed when we already
have specified how many characters it must read? That is because that though
we've told the function how much to read, it may well be less characters in
the file than we expected. We have that situation if we have ordered
readsome() to read 1024 characters and there are only 50 characters in the
file. So what happens then? readsome() reads ONLY 50 characters into our
string and then returns a value of 50 to tell us that it has read only 50
characters. That is how we know how long our string is.
Now about the other stuff. You know how to declare a variable, right?
What we do in my code, is following: we declare a variable:
This variable is an integer. In this variable we are going to store howCode:int bytes_read;
many characters readsome has read. Then we assign the variable the return
value of readsome():
Now, I instead of using two lines, I used just one, like:Code:bytes_read=b_file.readsome(str, 1024);
So this one line both declares a variable and assigns a value to it.Code:int bytes_read=b_file.readsome(str, 1024);
Why did I call the variable bytes_read? That is really not very important
right now, but what you actually do is tell readsome() how many bytes you
want to read from the file. But when we know that one character is one byte,
it is more or less the same (in our example) to say that we are telling
readsome() how many characters to get.
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
ok im getting confused again
int is a function and bytes_read is the variable where readsome stored what it read to?
im going back to the tutorial
Almost.. int tells the compiler that the variable bytes_read is an integer.Originally Posted by gothpunk
readsome() is a function. It stores number of characters it has read (an integer value) in the variable bytes_read.
The charcters that readsome() read themselves are stored in str varibale, which is parsed to the readsome() function.
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
I think this is a bit complex for a beginner. May be you should start with something else?
May be first learn some more basic C++, like declaring a variable, making a function, if-statements, loops and so on before getting involved with streams, memory distribution and pointers (which is basically what this all is about).
The OS requirements were Windows Vista Ultimate or better, so we used Linux.
Yeah I streams is a topic I still havent touched.. starts off basic but it can get complex when you actually use all its features.
It says to read the file one character at a time, for 1024 times or until it stops. It ends up stopping at 137 characters in this case (all that input you have in the file), then assign that number to a variable (bytes_read). 137 char's is still 137 letters (including spaces) so thats where he got that.. and he used the variable name bytes because 1 char is 1 byte right, 137 chars is 137 bytes. Guess saying bytes would be more correct since a space isnt a letter right.Code:int bytes_read=b_file.readsome(str, 1024);
So yeah dont know if that helps.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101