473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

read file help

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
3 1216
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
7
by: [ F e r n a n d o L o p e s ] | last post by:
Hello ALL, I need to read data from a .txt file and save this data in a sql server database table. The .txt file format like this: -----------------------------------------------------------...
15
by: ruca | last post by:
Hi, Can I read a .TXT File to a DataSet? How can I do that? I want to read his lines to a DropDownList. This lines are the names of employees that I export from an application that I have. I...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
3
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ...
6
by: | last post by:
Hi, I'm steel trying to read and update my XML file with Visual Basic Express but i am unable to find the right way to read my xml file and update it if neccessary... Here is my problem :...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
9
by: Hollywood | last post by:
Hello members of the comp.lang.c++, My log file is made of a set of 1000 following lines kind: 21/09/07 13:49:56,MW.SET.D_IGLS,2.000000 21/09/07 13:49:56,MW.SET.GNP_NT,7.000000 ..... ...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.