• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

C++: Reading from text file with numbers and strings

Joined
4/11/14
Messages
11
Points
13
My text file has structure in file "data.txt":
(1) Row 1: Symbol of Stocks.
(2) Row 2: Header of data.
(3) From Row 3: Only data in numbers.
I can just read the number data when I delete Row 1 and 2, following my code in "code.txt".
My purpose is reading all string data in row 1 and number data from row 3 to store in two separate arrays: Array SYMBOL to store symbol, and Array DATA to store number data but don't delete row 1 and row 2.

Thank you for your help.
Vinh Huynh
 

Attachments

  • Data.txt
    230 bytes · Views: 108
  • Code.txt
    1 KB · Views: 107
What's the question?
edit: I am guessing you want to know how to do it?

in C++ you could use

. streams
. boost matrices
. C++11 regex

for example.

BTW seems to be a memory leak in your code.
 
Last edited:
Hi Daniel Duffy, I am not good at C++ file handling with numbers and strings. I supposed you can edit my code. One more request is that do you know any article or book that guides to handling with data (inputs) ? It means that guides code in C++ to get data from text document (.txt) or excel file or any other storage file.
Thank you very much.
Vinh Huynh
 
Hi Daniel Duffy, I am not good at C++ file handling with numbers and strings. I supposed you can edit my code. One more request is that do you know any article or book that guides to handling with data (inputs) ? It means that guides code in C++ to get data from text document (.txt) or excel file or any other storage file.
Thank you very much.
Vinh Huynh
This is almost impossible given your current C++ level. I would search for packages that do this for you.

Off the top of my head, this package might be useful
Gawk for Windows
 
My purpose is reading all string data in row 1 and number data from row 3 to store in two separate arrays: Array SYMBOL to store symbol, and Array DATA to store number data but don't delete row 1 and row 2.

Why are you trying to store the string data in a double?
Make 2 separate arrays and store accordingly (detect data type).
 
Why are you trying to store the string data in a double?
Make 2 separate arrays and store accordingly (detect data type).
What do you mean? How to detect data with two separate functions? Can you show me?
Vinh Huynh
 
Back
Top