Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

17 replies to this topic
#1
Posted 05 July 2012 - 04:28 AM
Hi. I need to read a text file which is divided line by line and the lines are divided by 5 tabspaces. I need to group the parts between tabspaces. How can I read this kind of file tab by tab and line by line
#2
Posted 05 July 2012 - 05:03 AM
I suggest you read line by line and deal with the tabs per line
I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.
#4
Posted 05 July 2012 - 08:09 AM
Yeah, if you are working with C, you can use strtok.
But if you are working with C++, you can use getline method to get a line and to parse all the string in a line you can use istringstream class. Here is a hint.
But if you are working with C++, you can use getline method to get a line and to parse all the string in a line you can use istringstream class. Here is a hint.
ifstream file("example.txt"); if (file.is_open()) { while (!file.eof() ) { string line; getline (file,line); // reads a line cout << line << endl; istringstream iss(line); do { string sub; iss >> sub; // parting the each string in the line cout << "part-string: " << sub << endl; } while (iss); } file.close(); }
#5
Posted 06 July 2012 - 12:15 AM
Well I succeeded in reading line by line, now I need to extract the 3rd and 4th elements in the string. The elements are seperated by tabs. There are are 6 elements in total per line. Can you help me with that please?
I'm coding in C and the other elements are not used.
I'm coding in C and the other elements are not used.
#6
Posted 06 July 2012 - 05:39 AM
Look at the documentation for strtok:
http://cplusplus.com...cstring/strtok/
In the first call to strtok, you pass in your string pointer and a literal string containing your delimiters (\t in your case). Then, every subsequent call to strtok, while passing in null instead of your string pointer, will return the next token in the string. strtok returns null when there are no more tokens in the string to parse.
If you want to retrieve specific tokens in the string by their position, I would suggest implementing a while loop like the example code does in the documentation above, but also keep and increment an index variable each time the loop runs, and use that variable to store the parsed token into an array of string pointers. This way, each time the loop iterates, it points the next string pointer in your array to the next token. When you're all done, the 3rd and 4th elements will be accessible through the pointers at the array indices 2 and 3, respectively.
http://cplusplus.com...cstring/strtok/
In the first call to strtok, you pass in your string pointer and a literal string containing your delimiters (\t in your case). Then, every subsequent call to strtok, while passing in null instead of your string pointer, will return the next token in the string. strtok returns null when there are no more tokens in the string to parse.
If you want to retrieve specific tokens in the string by their position, I would suggest implementing a while loop like the example code does in the documentation above, but also keep and increment an index variable each time the loop runs, and use that variable to store the parsed token into an array of string pointers. This way, each time the loop iterates, it points the next string pointer in your array to the next token. When you're all done, the 3rd and 4th elements will be accessible through the pointers at the array indices 2 and 3, respectively.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 06 July 2012 - 10:01 AM
What are you using, C or C++? It dramatically changes the answer.
sudo rm -rf / && echo $'Sanitize your inputs!'
#8
Posted 06 July 2012 - 10:15 AM
I'm using C++
#9
Posted 06 July 2012 - 10:34 AM
In that case don't use strtok(). Can your file contain regular spaces in the fields as long as the fields are separated by tabs?
sudo rm -rf / && echo $'Sanitize your inputs!'
#10
Posted 06 July 2012 - 10:37 AM
the file is like this:
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
word1--tab--word2--tab--word3--tab--word4--tab--word5--newline
#11
Posted 06 July 2012 - 10:41 AM
So the words cannot contain spaces? In that case, do as kernelcoder suggested.
sudo rm -rf / && echo $'Sanitize your inputs!'
#12
Posted 06 July 2012 - 10:45 AM
words don't contain any spaces
Also tagged with one or more of these keywords: tab, file
Language Forums →
C and C++ →
How to implement password access to specific file type in Linux/Windows programmatically?Started by Kosarar, 08 Jan 2017 ![]() |
|
![]() |
||
Language Forums →
Java →
[HOMEWORK] Error Reading Files to Store their Data in an ArrayStarted by bodette, 26 Nov 2015 ![]() |
|
![]() |
||
Tutorial Forums →
Other Programming Tutorials →
Mobile Development Tutorials →
Creating a file browser in AndroidStarted by farrell2k, 16 Mar 2015 ![]() |
|
![]() |
||
![]() |
Language Forums →
Databases →
Access 2007 error while running .mdb fileStarted by Crilevral, 24 Feb 2015 ![]() |
|
![]() |
|
Language Forums →
Java →
How to write console output to a fileStarted by Alcamech, 29 Jan 2015 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download