2

Suppose I have two text files in.txt and out.txt.

in.txt:

abc      one    2    3    4
         two    3    4    two    twenty
         three  3    20   8
adbc     two    4    28   3      thirty

How can I read line-by-line and operate when necessary? So that on any line I can check if it has some text in the first column ("abc" and "adbc" in this example), what's in the second column, third column, etc. until that line ends? Then, based on those operations write into out.txt?

For example, if any line has its 6th column (if it has a 6th column) and it says "twenty" then print to out.txt "Hello."

or

If the second column is "three" add the following three numbers...

Bob John
  • 3,688
  • 14
  • 43
  • 57
  • 2
    Is "first column" sometimes empty? Do you know the column widths in advance? Or is white space your delimiter? It changes how you read things. – Floris Jan 22 '13 at 03:46
  • 2
    Do you have to use C? Text handling is easier in almost any other language. – Carl Norum Jan 22 '13 at 03:46
  • Yes, the first column is sometimes empty and the column widths are not known in advance. Unfortunately, I have to use C. – Bob John Jan 22 '13 at 03:47
  • So how do you know whether a token is in first or second column? – Floris Jan 22 '13 at 03:48
  • 1
    Similarly, (if the column widths are not known in advance and columns can be empty), ***how do you know*** whether “17        42” is columns three and four, very far apart (with column five empty), or columns three and five, with column four empty? – Scott - Слава Україні Jan 22 '13 at 03:56
  • What if we suppose that columns one and six are optional, yet all others are required? And there is a tab between columns. – Bob John Jan 22 '13 at 04:08
  • If the C requirement does not come from a homework assignment, you may be interested in that question: [Convert Python program to C/C++ code](http://stackoverflow.com/q/4650243/45249) (spoiler: the accepted answer is [Cython](http://cython.org/)) – mouviciel Jan 22 '13 at 10:15
  • If that is homework, please say so. Also get your requirements right; how are columns separated, space, tabs, both? And how is an empty column recognized? E.g. does every space-space or tab-tab means that there is an empty column? – Mecki Jan 22 '13 at 11:00

2 Answers2

0

I don't know about BEST way but here's one way:

Read the file in and use strtok to parse it split up by delimiters. (Then you can use strtok again to read it split up by whitespace)

http://www.cplusplus.com/reference/cstring/strtok/

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • See comments above... Strtok was my first thought but it doesn't work with empty columns... – Floris Jan 22 '13 at 03:50
0

I think strtok can still get the job done. Just follow two different paths based on lines beginning with spaces.

pseudo code:

while (line = readline()) {
    if (line[0] == ' ') { // you can check for multiple spaces if necessary.
        // If line begins with empty first column
        tokenize_handle_normal(line);
    } else {
        // If line begins with string in the first column
        tokenize_handle_special(line); // Call tokenize_handle_normal(line) after parsing the first column
    }
}
Ajith
  • 613
  • 1
  • 15
  • 32