Reading multiple data types from .txt in C

AI Thread Summary
The discussion centers around a programming challenge involving reading a text file containing atomic coordinates and chemical symbols. The user seeks assistance in extracting specific data types from each line of the file, specifically converting x, y, and z coordinates into doubles and the chemical symbol into a string. The user is familiar with reading lines into strings but is unsure how to parse and convert the data into the required formats. The problem is resolved when a colleague suggests using the fscanf() function, which effectively addresses the user's needs. The thread concludes with a request for deletion, indicating that the issue has been resolved.
Sdakouls
Messages
8
Reaction score
0
Hi,

I'm wondering if anyone would be able to help me with the following problem:

I have an input file in the form (values delimited by spaces):

0.0 0.0 0.0 Ga
0.5 0.5 0.5 As
1.0 1.0 1.0 In
.
.
.

and so on. (Basically it's a .txt containing x, y and z coordinates of atoms in a crystal lattice, along with the chemical symbol for the atom at a given lattice site.)

My aim is to try and write a program that would allow me to (for a given line of the file) to extract the data from this file so that I'd end up with (for line 2 in the above, for example):

x = 0.5
y = 0.5
z = 0.5
atom = As

where, x, y and z are doubles and atom is char* (or a string... I'm not fussy).

I'm familiar with the usual "reading an entire line of a file into a string" approach, but I'm not sure how to proceed in this instance since I need to take different parts of the line as different datatypes.

I've tried googling the problem and reading around, but I couldn't find anything hugely relevant.

Any help would be greatly appreciated.
 
Technology news on Phys.org
fscanf()
 
Problem solved by a helpful officemate.

Mods: Delete thread.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top