Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
this is the data in datafile.txt,
line:30,
num0=1
num1=3
num2=2
...
num29=5

I want to import the value of num0 to num29 to array[29],how can I realize this,thank you !
Posted
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 22:42pm    
What do you try so far?
--SA
Angela2012 22-Mar-12 23:13pm    
hi,SAKryukov,I have used CFileDialog to read the txt file. but i don't know how to only extract the numbers
Sergey Alexandrovich Kryukov 23-Mar-12 1:07am    
I don't think you did it, because CFileDialog does not read files. Please see my answer with references on working with files.
--SA

You can do this using sscanf as below. But you should know the format of each line. here we assume that lines either get formatted like
"line:30,"
or
num0=1
so we write our code as below

C++
CString csLine; // csLine has each line text
int c, d;
if( !sscanf( csLine, "num%d=%d", &c, &d ) // c will have 0 and d will have 1 incase of num0=1
{
    if( !sscabf( csLine, "line:%d", &c ) // c will have 30 incase of "line:30"
    {
        // Some incorrect/unexpected format for the line
    }
}


hope you got the idea
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-12 1:08am    
My 5, but it looks like OP has no idea how to work with files in principle (please see comments to the question).
I added some links in my answer, credited yours.
--SA
Angela2012 23-Mar-12 4:41am    
Thank you very much,you are so kind. I solve my problem with your solution.
I tried ...fscanf(file,"%*[^=] = %f",&num)...in MFC. and it worked.
First, you need to know how to read files. Please see:
C++ style: http://www.cplusplus.com/doc/tutorial/files/[^];
C style: http://www.cplusplus.com/forum/beginner/32690/[^].

You can also use the sscanf as demonstrated by Resmi Anna:
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/[^].

—SA
 
Share this answer
 
Comments
Resmi Anna 23-Mar-12 1:22am    
Thats great...A 5 :-)
Sergey Alexandrovich Kryukov 23-Mar-12 1:29am    
Thank you,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900