473,785 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q on loading txt file in C++, and search algorithm

4 New Member
Hello, I am a very newbie (big emphasis on being a newbie) to c++. I read c++PRIMER but apparently, it did not provide much help.
I need a bit of help in codes loading txt file in C++. My frustration is that the file does not have any names of variables. The data looks as follows:
01/02/2004,1108.48
01/05/2004,1122.22
How do I assign the name to the variables in C++? (ie. how do I let C++ know that "Date" is the first column and "price" is the second colum?) Also, how do I categorize the date with " / " ? Float does not seem right, and using Char may cause problem too. (I need to compare the dates and find max, min, etc) I checked some postings, and one of them said to use stringstream, but I am not sure how to do so.

Also, I need to build a search "algorithm" that will have two variables, Date1 and Date2. And for any dates in between Date1 and Date2, I need the list of Dates and matching price displayed. Any hints which function I should use?

Any help would be appreciated.

Thanks:)
Sep 23 '07 #1
3 2009
weaknessforcats
9,208 Recognized Expert Moderator Expert
The disc file is just bytes of data. What you have to do is map those bytes to variables in your program. For example:

01/02/2004,1108.48

Your layout is:

int 01
char /
int 02
char /
int 2004
char ,
double 1108.48

So just use cin>> with variables of the correct type in this exact order.

Take a run at it and let me know how you get on.
Sep 23 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
So just use cin>> with variables of the correct type in this exact order.
Oops. I meant create an ifstream object and use it. cin is for keyboard input.
Sep 23 '07 #3
mintominto82
4 New Member
Oops. I meant create an ifstream object and use it. cin is for keyboard input.
Expand|Select|Wrap|Line Numbers
  1. ifstream file("sample.txt");
  2. file.open("sample.txt");  
  3.                 in >> int month;    
  4.                 in >> char /; 
  5.                 in >> int day;
  6.                 in >> char /; 
  7.                 in >> int year;
  8.                 in >> char , ; 
  9.                 in >> float price;
I did try, except it keeps giving me "expected primary-expression before "int" " error (same for char and float) when I try to compile. ???? And when and if I succeed in doing this, how does my data look like?

Does it look like matrix with 7 columns with month, " / ", day, " / ", year, " , ", price each column vector with 252 entries?

Also, how can I write these variables into arrays? there are 252 lines, and I want the columns to be month, day, year, price.
so it should be array[5][253]?

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     float Array[i][j];
  3.     for (int i = 0; i < 5; i++)
  4.     for (int j = 0, j <253, j++)
  5.     {
  6.         ???
  7.  
  8.         }
  9.  
  10.     return 0;
  11.  
  12. }
I tried to do it, but it is very confusing. what should be in "???" place in order to successfully produce an array?

any help is appreciated. thanks
Sep 25 '07 #4

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

Similar topics

10
3141
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x"; char search = '?'; char* replace = "***"; char* result = search_and_replace(source,search,replace);
6
1853
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some of the ways of doing this are 1. Read each line and do string compare immediately. 2. Read each line, add each string into a stl vector and search for the given string using stl bineary_search algorithm.
3
5114
by: kittykat | last post by:
Hi, I was wondering if you could help me. I am writing a program in C++, and the problem is, i have very limited experience in this language. I would like my user to enter a specific pattern, and I want my program to search a text file for this pattern, and let the user know if this pattern exists or not. So far, i have figured out how to make my prgram read the text file, but i'm not sure how to take the information the user inserts...
5
4024
by: Olaf Gschweng | last post by:
We're new into DB2 and have some problem with DB2 8.1 (?) on a Linux system. We load some big tables of a DB2 database from files every day. We do a "DELETE FROM table" for each table and then we load the data by something like that: LOAD FROM "/pathto/artikel.data" OF DEL MODIFIED BY DATESISO CHARDEL0xbf SAVECOUNT 50000 MESSAGES "/pathto/artikel.msg" INSERT INTO artikel;
28
3179
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple linear search the best here(for loop from beginning to end of array)? It seems like some other search algorithm like binary or whatever would be of no benefit on this data set.
60
49196
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't be indexed/sorted. I don't want to load the entire file into physical memory, memory-mapped files are ok (and preferred). Speed/performance is a requirement -- the target is to locate the string in 10 seconds or less for a 100 MB file. The...
4
3383
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the files could be upwards of 25mb in size and time is important. I don't want to take the route of loading the text of the file into a giant string and searching it, but would rather focus on a performance-minded solution. Any sugesstions for a...
4
5349
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given the Identifier. The logical way would be to read the record once and put it in an STL container such as vector and then use lower_bound to search for a given identifier. But for some strange reason i'm asked to not use a container but instead...
6
12182
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching though a lot of information to find a match, the first idea that comes to mind is the linear search. Loop through all the values looking for a match. If you find it, return the location, or value and end the search. However this becomes highly...
0
10327
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
10151
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
10092
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
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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
5381
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.