Reading multiple data types from .txt in C

In summary, the conversation is about a person seeking help with extracting data from an input file containing coordinates and chemical symbols for atoms in a crystal lattice. They are looking for guidance on how to handle different data types in a single line of the file. The issue was eventually resolved with the help of a colleague and the thread was requested to be deleted.
  • #1
Sdakouls
8
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
  • #2
fscanf()
 
  • #3
Problem solved by a helpful officemate.

Mods: Delete thread.
 

1. How do I read multiple data types from a .txt file in C?

To read multiple data types from a .txt file in C, you can use the fscanf() function. This function allows you to specify the data types and variables that you want to read in from the file.

2. What is the format specifier for reading a string from a .txt file in C?

The format specifier for reading a string from a .txt file in C is %s. This will read in a sequence of characters until it encounters a space or a newline character.

3. Can I read in data from a .txt file in C if the data types are not known beforehand?

Yes, you can use the generic format specifier %c to read in characters from a .txt file in C. You can then convert these characters to the appropriate data type using functions such as atoi() or atof().

4. How do I handle errors while reading data from a .txt file in C?

You can use the return value of the fscanf() function to check for errors while reading data from a .txt file in C. If the function returns a value less than the number of variables specified, it indicates that an error has occurred.

5. Is there a limit to the number of data types that can be read from a .txt file in C?

No, there is no limit to the number of data types that can be read from a .txt file in C. However, it is important to ensure that the number of variables specified in the fscanf() function matches the number of data types in the file to avoid errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
463
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
741
  • Programming and Computer Science
Replies
5
Views
1K
Replies
10
Views
896
  • Programming and Computer Science
Replies
19
Views
1K
  • Electrical Engineering
Replies
1
Views
699
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top