Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

scanf - Problem with using delimiters in C with fscanf

So lets say I have a line of text in a txt file (e.g. 1234567890 DD.M.YYYY. hh:mm NAMEOFHALL) and i have around 2000 of said lines. I would like to use fscanf to pull out "1234567890", ""DD.M.YYYY", "HH:MM" and "NAMEOFHALL" as separate strings (note: NAMEOFHALL does not have a fixed length, it can vary from something short as A203 to FSB HALL B).

Here's the bit of code I'm using for scanning and seeing how the 4 separate strings look like:

while(i<lines){
    fscanf(fp,"%11[^ ] %10[^ ] %5[^ ] %[^
]", polaznik[i].jmbag, polaznik[i].datum, polaznik[i].vrijeme, polaznik[i].dvorana);
    printf("%s#%s#%s#%s
",polaznik[i].jmbag, polaznik[i].datum, polaznik[i].vrijeme, polaznik[i].dvorana);
    i++;  
}

where fp is a pointer to the .txt file

You can see the full code here.

I used hashtags in printf because I thought that there were some stray space characters in the read but there are not any.

I am expecting an output from the printf like:

0246694342#25.1.2014.#12:16#FSB Dvorana C

But instead I get:

0246694342FSB Dvorana C#25.1.2014.#12:16#FSB Dvorana C

Also please refrain from subtly roasting me as I am doing this on low energy and I'm more prone to miss obvious mistakes that way.

Thanks to anyone that replies.

question from:https://stackoverflow.com/questions/65838360/problem-with-using-delimiters-in-c-with-fscanf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...