473,503 Members | 11,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading information in a file

3 New Member
hey i'm tyring to prep for an exam and i'm really unsure of what i'm doing

we have to write a programthat will read a text file and then get the program to preform specific actions based on the information in the file,

i can get the program to read the file no problem but i really don't know how to get it to read the information so that it can preform different tasks for the different information given (the file contains both words and figures)

any help or advice that anyone could offer would be much appreciated!!

p.s. sorry if this is a bit of an ameturish question but i've tried books and other sites on the net and i'm getting nowhere
May 15 '06 #1
5 1776
Banfa
9,065 Recognized Expert Moderator Expert
Once you have read the the from the file then you need to parse that text.

Suppose that you read the following line from the file into an array of characters called line.

print: Hello World

OK and suppose that the program has to print anything that follows the print: statement to the screen you could achieve that this way

Expand|Select|Wrap|Line Numbers
  1. char line[MAX_LINE];
  2.  
  3. /* file opened and line read here */
  4.  
  5. if ( strncmp("print:", line, 6) == 0 )
  6. {
  7.     puts( &line[6] );
  8. }
  9. else ... /* Look for other commands in the file */
  10.  
  11.  

You could also look up the help for strtok although this function has some pit falls of it's own.
May 16 '06 #2
viadd
3 New Member
I've gotten the file to print the informaton in it but its not just characters its numbers as well.

This is an example of information that would be in a the file.

2000 Airbus 200
1999 Boeing 100
1998 Boeing 200
2004 Airbus 300
2003 Boeing 250
2001 Airbus 250
2002 Boeing 200
2003 Boeing 150

I've got to sort it by the year of construction type of plane and number of passengers using an algorithm that I've been given.

The problem is that I don't even know where to begin, how can i get the program to differentate between the two different types of figures and the type of plane?

Any help at all would be great as I'm completely lost!!
May 16 '06 #3
iLL eFFect
2 New Member
u can do this in two ways:
1. if u are writin the file as well then u can feed the data while writin, in a structure and write in the file.
2. u can write character by charcter and look for numeric values. soon as u find it look for alphabet and then numeric value again.

does this solve ur problem?
May 16 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
In that case you should probably look up the function

strtoul (or strtol)

This function converts text based digits into an integer and returns a pointer to where it stopped converting the string, allowing you to continue parsing from there.
May 16 '06 #5
viadd
3 New Member
u can do this in two ways:
1. if u are writin the file as well then u can feed the data while writin, in a structure and write in the file.
2. u can write character by charcter and look for numeric values. soon as u find it look for alphabet and then numeric value again.

does this solve ur problem?
The problem is that i don't really know how to do those things, but they sound like the sort of direction i want to be heading in.

That stroul function looks pretty promising,.Thanks Banfa I'm gonna give it a go now and see if I can get it to work for what I want to do.
May 16 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
7068
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
7
2543
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
7
6032
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
2169
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
0
1890
by: EPDJ | last post by:
Well, I have to edit a program which we previously used for reading a header file to be able to read a trailer from a .dat file. The .dat file consists of 5 entries in each record and I am assuming...
1
2104
by: une | last post by:
hey guys, I have this program to do, but I kind of started forst just te read things from the file and then I would go and make the calculation but it is showing some wierd funnky resualts, I dont...
8
2319
namcintosh
by: namcintosh | last post by:
I really need some help. I am trying to read some information from a file in C++. Here is the program that I wrote. (Beware, I am very new to this, so don't freak out if the program seems a...
21
626
by: Naya | last post by:
Hello, everyone!!! Well, I have a situation here. I am trying to read this data from a file, but the wrong values keep spitting out at me. Here's what I mean: Program: int main() {...
6
4212
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
3
4074
by: itmfl | last post by:
We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is...
0
7212
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
7296
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
7364
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
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
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...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.