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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top