| | |
reading input ... quick C++ question ...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 31
Reputation:
Solved Threads: 0
ok...i am trying to figure out how to read input ....
i am reading input from the command line
within the input_file, it looks like this:
and im looking to read in the whole thing .... in each line, the first number is the item #, and the rest of the numbers is some attributes for that item number ... and each line is a different item # ...
i know how to get it to read in the whole file:
but how do i get it to read in until the end of the line (every line can have a different number of attributes)?
i was going to have another while loop within it?
here is some code:
do i read until i find \n ?
??
any help would be great..thanks
i am reading input from the command line
C++ Syntax (Toggle Plain Text)
sh% ./a.out < input_file
within the input_file, it looks like this:
C++ Syntax (Toggle Plain Text)
0 4 6 7 3 1 3 6 7 4 3 6 2 3 6 7 3 2 4 6 7 7 3 5 4 3 2 9 1
and im looking to read in the whole thing .... in each line, the first number is the item #, and the rest of the numbers is some attributes for that item number ... and each line is a different item # ...
i know how to get it to read in the whole file:
C++ Syntax (Toggle Plain Text)
while (!cin.eof()) { //reads in everything until the end of the file }
but how do i get it to read in until the end of the line (every line can have a different number of attributes)?
i was going to have another while loop within it?
here is some code:
C++ Syntax (Toggle Plain Text)
#include<iostream> int main() { int input; while (!cin.eof()) { //reads in everything until the end of the file while (?????) { //reads in each line } }
do i read until i find \n ?
C++ Syntax (Toggle Plain Text)
while (input != '\n')
??
any help would be great..thanks
Greetings jaeSun,
There are a few ways you could accomplish this task. I do see you are using iostream, so I'll stick with its functionality.
Both are uesful, and can be relied on for different projects. Let me explain both.
Using the while loop
When using the while loop with a stream, you may be familiar with eof(). All eof stands for is End Of File. Since we are dealing with iostream, we are going to use its function get().
int get();
get() extracts a character from the stream and returns its value.
Firstly, I will split the code in sections for further detail.
This simply defines we are to use these header files in our program. fstream provides a stream interface to read and write data from/to files.
» The function open() opens a file. The stream's file buffer is associated with the specified file to perform the i/o operations.
» Instead of calling eof(), we will call good() as it checks if stream is good for i/o operations. The function returns true if none of the stream's error flags are set.
» Next we call get(), as it gets the next character in the stream indefinetly in our while loop until it breaks.
» After that, we close the file.
Use the iostream read() function
This way of reading a file is a bit more complex, but is a more efficent way of reading the stream into memory.
There are some new functions used like seekg() and tellg().
» seekg() sets the position of the get pointer. The get pointer determines the next location to be read in the buffer associated to the input stream.
istream& seekg( streamoff off, ios_base::seekdir dir);
» tellg() returns the position of the get pointer. The get pointer determines the next location to be read in the buffer associated to the input stream.
So its simple on how our process for the length of our stream worked. Go to the end of the file, get the length, take it back to the beginning and read it.
» The new and delete keywords are to help you allocate memory use at runtime. The keyword new will allocate a chunk of memory within your program and delete will deallocate it. You will probably also want to keep track of these chunks by using pointers.
» Once we are done, we read the stream into our newly allocated buffer with the specified amount of length for our string.
istream& read (char* s, streamsize n);
read() reads a block of data of the length specified by n. Data is read sequentially, so if the End-Of-File is reached before the whole block could be read the buffer will contain the elements read until End-Of-File.
Conclusion
I hope this information has been useful. If you have further questions, please feel free to ask.
- Stack Overflow
There are a few ways you could accomplish this task. I do see you are using iostream, so I'll stick with its functionality.
- Use the while loop
- Use the iostream read() function
Both are uesful, and can be relied on for different projects. Let me explain both.
Using the while loop
When using the while loop with a stream, you may be familiar with eof(). All eof stands for is End Of File. Since we are dealing with iostream, we are going to use its function get().
int get();
get() extracts a character from the stream and returns its value.
Firstly, I will split the code in sections for further detail.
#include <iostream> #include <fstream> using namespace std;
int main () { char c, str[256]; ifstream is; // Open file is.open("Your file"); // Loop while extraction from file is possible while (is.good()) { // Get character from file c = is.get(); cout << c; } // Close file is.close(); return 0; }
» Instead of calling eof(), we will call good() as it checks if stream is good for i/o operations. The function returns true if none of the stream's error flags are set.
» Next we call get(), as it gets the next character in the stream indefinetly in our while loop until it breaks.
» After that, we close the file.
Use the iostream read() function
This way of reading a file is a bit more complex, but is a more efficent way of reading the stream into memory.
#include <iostream> #include <fstream> using namespace std; int main () { int length; char *buffer; ifstream is; is.open("Your file", ios::binary); // Get length of file is.seekg(0, ios::end); length = is.tellg(); is.seekg(0, ios::beg); // Allocate memory buffer = new char[length]; // Read data as a block is.read(buffer, length); // Close file is.close(); // Print data cout << buffer << endl; // Free memory delete []buffer; return 0; }
» seekg() sets the position of the get pointer. The get pointer determines the next location to be read in the buffer associated to the input stream.
istream& seekg( streamoff off, ios_base::seekdir dir);
- ios_base::beg - beginning of sequence.
- ios_base::cur - current position within sequence.
- ios_base::end - end of sequence.
» tellg() returns the position of the get pointer. The get pointer determines the next location to be read in the buffer associated to the input stream.
So its simple on how our process for the length of our stream worked. Go to the end of the file, get the length, take it back to the beginning and read it.
» The new and delete keywords are to help you allocate memory use at runtime. The keyword new will allocate a chunk of memory within your program and delete will deallocate it. You will probably also want to keep track of these chunks by using pointers.
» Once we are done, we read the stream into our newly allocated buffer with the specified amount of length for our string.
istream& read (char* s, streamsize n);
read() reads a block of data of the length specified by n. Data is read sequentially, so if the End-Of-File is reached before the whole block could be read the buffer will contain the elements read until End-Of-File.
Conclusion
I hope this information has been useful. If you have further questions, please feel free to ask.
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Oct 2004
Posts: 31
Reputation:
Solved Threads: 0
ok...so then i can do this: ?
??
C++ Syntax (Toggle Plain Text)
#include<iostream> int main() { char input[200]; cin.getline(input, 200); while (cin.good()) { //modify the character line as needed //(i will need to convert from char to int value) //which i can use another while loop to pull the first character, //get the digit, get the next character, get the digit, until //i find a space ... then get the next number, until I find a NULL //which means its at the end of the line (the getline() replaces //the \n with a NULL ?? cin.getline(input, 200); } return 0; }
??
Checking the line input using getline() or eof() is fine. There are two possible ways of doing this:
Ah, a third parameter for getline().
» istream& getline (char* s, streamsize n, char delim);
If the delimiter is found it is extracted but not not stored. Use get() if you don't want this character to be extracted. This parameter is optional, if not specified the function considers '\n' the delimiter.
Instead of using the while loop on eof(), we could just use getline() instead:
Either way it accomplishes the same task.
- Stack Overflow
#include <iostream> using namespace std; int main() { char input[200]; cin.getline(input, 200, '\n'); while (!cin.eof()) { // Do stuff here... cin.getline(input, 200, '\n'); } return 0; }
» istream& getline (char* s, streamsize n, char delim);
If the delimiter is found it is extracted but not not stored. Use get() if you don't want this character to be extracted. This parameter is optional, if not specified the function considers '\n' the delimiter.
Instead of using the while loop on eof(), we could just use getline() instead:
#include <iostream> using namespace std; int main() { char input[200]; while (cin.getline(input, 200, '\n')) { // Do stuff here... } return 0; }
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
If you can use stringstreams, then you can just use getline for each line and then use a stringstream of the line to pick off the ints.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> int main() { std::string line; while ( std::getline(std::cin,line) ) { std::istringstream iss(line); int value; while ( iss >> value ) { std::cout << value << ','; } std::cout << '\n'; } return 0; } /* my output C:\Test>testpp < file.txt 0,4,6,7,3, 1,3,6,7,4,3,6, 2,3,6,7,3,2,4,6,7,7, 3,5, 4,3,2,9,1, */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 31
Reputation:
Solved Threads: 0
ok, your going to have to bear with me (sorry, i feel like im running with my head cut off...hw assignment is due tomorrow, and yes, i should have started earlier) ......
i was getting each line, and then using the strtok function to break it up into each of its own ... and then compare the char and convert to int ..... didnt work ....
this is the code i had:
it works, prints out all the numbers into the line ... BUT, when it gets to the end, it gives a segmentation fault ...
i thought when it couldnt strtok anymore (basically, reaches the end of the line), it returns a NULL. but i guess i cant compare tokout to NULL ?? (compiler says cant compare tokout to int, or im guessing its saying that i cant compare a string to a NULL ..
i did a similiar thing in C and it worked, but guess not in C++ ??
questions on stack overflow's code:
1. in your while loops, what if there is only 1 line, thus, no \n at the end? will it still process that line? im guessing it will, since it says to go 200 characters, so it will do whichever comes first, or in the case of no line, go as far as it can?
questions for dave:
1. ok, ive never messed with the sstream's at all ... actually, never heard of it until now ....and im having a hard time understanding your code ...
ive tested it out... and it works ... i can add and subtract ...but im having a hard time distinguishing when it goes to the next line ....
grrrrr
i was getting each line, and then using the strtok function to break it up into each of its own ... and then compare the char and convert to int ..... didnt work ....
this is the code i had:
C++ Syntax (Toggle Plain Text)
char input[200]; string tokout; int num; cin.getline(input, 200); cout <<input<<endl; //prints out the whole line of numbers tokout = strtok(input," \n\t"); //gets the first number cout <<tokout<<endl; //prints the first number while (tokout != NULL) //while loop to get the rest of the numbers { tokout = strtok(NULL," \n\t"); cout <<tokout<<endl; } cout<<endl;
it works, prints out all the numbers into the line ... BUT, when it gets to the end, it gives a segmentation fault ...
i thought when it couldnt strtok anymore (basically, reaches the end of the line), it returns a NULL. but i guess i cant compare tokout to NULL ?? (compiler says cant compare tokout to int, or im guessing its saying that i cant compare a string to a NULL ..
i did a similiar thing in C and it worked, but guess not in C++ ??
questions on stack overflow's code:
1. in your while loops, what if there is only 1 line, thus, no \n at the end? will it still process that line? im guessing it will, since it says to go 200 characters, so it will do whichever comes first, or in the case of no line, go as far as it can?
questions for dave:
1. ok, ive never messed with the sstream's at all ... actually, never heard of it until now ....and im having a hard time understanding your code ...
ive tested it out... and it works ... i can add and subtract ...but im having a hard time distinguishing when it goes to the next line ....
grrrrr
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[200], *tokout;
int num;
while ( cin.getline(input, 200) )
{
cout << input << endl; //prints out the whole line of numbers
tokout = strtok(input," \n\t"); //gets the first number
while ( tokout != NULL ) //while loop to get the rest of the numbers
{
cout << tokout << endl; //prints the first number
tokout = strtok(NULL," \n\t");
}
cout<<endl;
}
return 0;
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Yes,
You can learn more about stringstream, and other C++ streams such as fstream, iostream, and streambuf at: http://cplusplus.com/ref
Or just learn more about stringstream here.
Hope this helps,
- Stack Overflow
You can learn more about stringstream, and other C++ streams such as fstream, iostream, and streambuf at: http://cplusplus.com/ref
Or just learn more about stringstream here.
Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Other Threads in the C++ Forum
- Previous Thread: Why does my function not a member?
- Next Thread: Establishing A connection between Three programs.
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






