473,657 Members | 2,550 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 1786
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,.Than ks 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
7086
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 disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
7
2553
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 it's buisness. My problem is all the resources I have found aren't very clear on these things. All of them open the file then check to see if the stream is open. Well, the problem with using file.open("filename.dat", ios::in | ios::binary) is...
7
6055
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
1
2176
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 table names that are in the XML file, but I'm not able to access the rows. Here's the code that I've got - it assumes that the fileName passed in already exists: Public Sub GetInput(ByVal fileName As String) dsFileCopy = New...
0
1904
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 atleast 5 records(which I believe the amount of records should be taken from the trailer). Also I believe he said the SENTINEL value must be -1. Our instructor gave us an extremely limited amount of information on how to attempt to do this and its...
1
2116
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 know why is now outputing what is in the file, this is the program A file called Stock.txt contains information regarding the 9 different stock items carried by FUNNY STUFF RETAIL INC. The file is a repeating sequence of three records such that the...
8
2327
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 little off). #include <iostream> #include <conio> #include <fstream> #include <string> using namespace std;
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() { ifstream inFile;
6
4224
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 function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
3
4084
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 formatted like so: /beginning/ matrix1 3 2 1 6 2 4 3 5 matrix2
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
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.