Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Reading From A File Tab By Tab

tab file

  • This topic is locked This topic is locked
17 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

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
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 05 July 2012 - 05:03 AM

I suggest you read line by line and deal with the tabs per line
  • 0

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.


#3 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 05 July 2012 - 05:22 AM

strtok(str, "\t") will be useful to you here.
  • 0

ti-99-sig.png
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


#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

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.
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();
}

  • 0

#5 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

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.
  • 0

#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

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.
  • 0

ti-99-sig.png
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 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 06 July 2012 - 10:01 AM

What are you using, C or C++? It dramatically changes the answer.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#8 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 06 July 2012 - 10:15 AM

I'm using C++
  • 0

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

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?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

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
  • 0

#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 06 July 2012 - 10:41 AM

So the words cannot contain spaces? In that case, do as kernelcoder suggested.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#12 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 06 July 2012 - 10:45 AM

words don't contain any spaces
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download