By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,054 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,054 IT Pros & Developers. It's quick & easy.

read file help

P: n/a
There is a file name file.txt with content below:
11 21 31 41
41 31 21 11
31 21 11 41

How can I read the file line by line, and then print the first three
columns(separed by tab)?

Thanks for any idea.

Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"George" <@writes:
There is a file name file.txt with content below:
11 21 31 41
41 31 21 11
31 21 11 41

How can I read the file line by line, and then print the first three
columns(separed by tab)?

Thanks for any idea.
What have you tried? Post your code, and we might be able to help you
with it. (No, we won't do your work for you.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #2

P: n/a
In article <48********@127.0.0.1>, George <@wrote:
>There is a file name file.txt with content below:
11 21 31 41
41 31 21 11
31 21 11 41
>How can I read the file line by line, and then print the first three
columns(separed by tab)?
awk or perl would probably work well for this kind of application.

Your question is ambiguous; I see at least three possible
meanings:

1) print all of the members of the first column, *across*,
then print a tab, then print all of the members of the second
column *across*, then a tab, then all of the members of the
third column (still *across*.)

2) similar to the above, but with tabs between all the values

3) print the first member of the first column, then a tab,
then the first member of the second column, then a tab,
then the first member of the third column, then a newline,
print the second member of the first column, then a tab,
then the second member of the second column, then a tab,
then the second member of the third column, then a newline,
and so on, proceeding down the columns.
Also, it is ambiguous as to whether for some reason you need to
read the entire file before starting the printing, or if it would
be acceptable to print as you go.
--
"Nothing recedes like success." -- Walter Winchell
Jun 27 '08 #3

P: n/a
George wrote:
There is a file name file.txt with content below:
11 21 31 41
41 31 21 11
31 21 11 41

How can I read the file line by line, and then print the first three
columns(separed by tab)?
Do you have to read them a line at a time? If not then I suggest reading
the file through a character-at-a-time function like getc() and
discarding the last whitespace sequence and the numerical characters. A
counter would be needed to keep track of how many columns you have
printed.

There are other possible ways depending on the exact format of your
lines, and whether they are fixed or might vary. If you have attempted
a solution you could post the code here for further assistance.

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.