How to Extract Atomic Numbers from a Text File into an Array in C?

  • Thread starter Thread starter TruthOasis
  • Start date Start date
  • Tags Tags
    Array File
Click For Summary

Discussion Overview

The discussion focuses on extracting atomic numbers and other related data from a text file into arrays using C programming. Participants explore various methods for reading and storing data, addressing issues related to file handling and array indexing.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in reading atomic numbers from a text file and storing them in an array.
  • Another suggests using the scanf() function for input, prompting questions about its application in file reading.
  • A later reply introduces fscanf() as a method to read from a file, providing an example of its usage.
  • Participants discuss the importance of correctly using the address operator & with scanf() and fscanf().
  • There are suggestions to check the return value of fscanf() to ensure successful reading of data.
  • One participant expresses confusion about array indexing and initialization, leading to further clarification on handling arrays of strings and integers.
  • Another participant mentions the need to handle the end of the file and the potential for blank lines in the input data.
  • Eventually, a participant reports resolving their issues with the loop and successfully extracting the data.

Areas of Agreement / Disagreement

Participants generally agree on the methods to read data from a file using fscanf(), but there is some confusion regarding array indexing and initialization. The discussion includes multiple approaches and suggestions, with no single consensus on the best method.

Contextual Notes

Participants note limitations regarding the robustness of the current approach, suggesting that a more sophisticated method may be necessary for handling various data formats and ensuring error checking.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in file I/O operations and data manipulation in arrays.

TruthOasis
Messages
7
Reaction score
0
Hi, I need help taking the atomic numbers from a list of elements and putting them in an array.
I have my data in a text file in the form:
Ac
227
Actinium
89

Al
26.981539
Aluminium
13

Am
243
Americium
95
etc.

This is what I am have so far:
FILE *fp;
char myStr[1000];
char atomicnumber[1000];
int i;
int j=0;

fp = fopen("elements.txt","r");
for (i=0;i<600;i++)
{
j++;
fgets(myStr,600,fp);
if ((i) % 3 ==5)
{
myStr=atomicnumber[j];
}

}
fclose(fp);
How would I get those numbers into an array like atomicnumber[1000]={89,13,95...} ?

Any help would be greatly appreciated.
 
Technology news on Phys.org
take a look at scanf()
 
doesn't scanf() just take input from the user? How do I incorporate that into the program? Would i put in my for loop: scanf(%d,myStr) ?
 
The man page for scanf should also mention fscanf() to read form a FILE* and sscanf() to read from a string.

you would put something like
char abv[2];
float mass;
char name[32];
int number;
scanf("%s%f%s%d",abv,&mass,name,&number); // note the "&"

Try this first, then worry about readign them into arrays.
 
Ok, i tried to fix it but it isn't working correctly. I get a whole bunch of mess when I try to print the vector.
int main(void)
{
int i;
char elementSymbol[1000];
int atomicNumber[1000];
char elementName[1000];
float atomicMass[1000];

FILE *fp;

fp = fopen("elements.txt","r");
for (i=0;i<600;i++)
{
fscanf(fp,"%s\n%f\n%s\n%d",elementSymbol,&atomicMass,elementName,&atomicNumber);
printf("%s\n%f\n%s\n%d",elementSymbol,&atomicMass,elementName,&atomicNumber);

}
fclose(fp);
system("PAUSE");
return 0;
}
 
printf("%s\n%f\n%s\n%d",elementSymbol,&atomicMass, elementName,&atomicNumber);
The & means take the address of - so you are trying to print the address of atomicMass as a float instead of the value of atomicMass. You need the '&' on scanf because you are reading data - so you need the address to tell scanf where to put it.

Your arrays are also a little confused.
"char elementSymbol[1000];" - means an array of 1000 chars, or a string 999chars long
"int atomicNumber[1000];" - means an array of 1000 ints, for now just put atomicNumber and atomicMass.
 
when I set the size of my array, shouldn't i just make it a really big number so the amount of characters or numbers don't exceed that size, until I learn how to use dynamic allocation?
Also, I can't just forget about the abbreviation and name because when I use the fscanf function I need to use the \n to tell it to go from line to line.

this is what I have right now I'm not sure what else to do.
int i;
char elementSymbol[500][3];
int atomicNumber[1000];
char elementName[500][20];
float atomicMass[1000];

FILE *fp;

fp = fopen("elements.txt","r");
for (i=0;i<545;i++)
{
fscanf(fp,"%s\n%f\n%s\n%d",elementSymbol,&atomicMass,elementName,&atomicNumber);
printf("%s\n%f\n%s\n%d",elementSymbol,atomicMass,elementName,atomicNumber);

}
fclose(fp);
 
I meant don't worry about storing the array for now, just have variables for one entry:
Code:
char elementSymbol[3];
int atomicNumber;
char elementName[20];
float atomicMass;

But what you have is close - so carry on!
How do you know how many entries are in the file?
You are allocating space for 500 of them but reading 545 of them?
fscanf returns the numbe rof entries it matched, so you can use this to check if it worked.
You should also check the value of 'fp' after the open to make sure the open worked - otherwise it will crash at the first fscanf()
 
Cool! I took it out of the for loop and removed the and got the first 4 values. So the file is opening correctly.

fp = fopen("elements.txt","r");
fscanf(fp,"%s\n%f\n%s\n%d",elementSymbol,&atomicMass,elementName,&atomicNumber);
printf("%s\n%f\n%s\n%d\n",elementSymbol,atomicMass,elementName,atomicNumber);

display:
Ac
227.000000
Actinium
89
Press any key to continue . . .but now when I try to put it back in the for loop and put the after each variable so I go the the next component it doesn't cycle correctly

btw, I have most of the elements and I calculated I had 545 lines
 
  • #10
You were very close - with the array index
Code:
for (i=0;i<545;i++)
{
fscanf(fp,"%s\n%f\n%s\n%d",elementSymbol[i],&atomicMass[i],elementName[i],&atomicNumber[i]);
printf("%s\n%f\n%s\n%d",elementSymbol[i],atomicMass[i],elementName[i],atomicNumber[i]);
}
The second dimension on the strings is sort of handled automatically - think of elementSymbol[500][3] as an array of 500 strings of length [3]

You probably want to check if fscanf returns 0 for end of file.
You might also need another '\n' at the end of fscanf to skip the blank line.

This isn't the most robust way of doing this - in reality you would read each line and decide what it was then convert it into the correct type.
fscanf() is very useful if all the parts of the record is on one line.
 
  • #11
Hmmm.. when I try doing that I get an error: subscripted value is neither an array nor a pointer

this only happens when i add the to the atomic mass and atomic number.
Is this because i didn't initialize them correctly? Do I need to set up a pointer variable?
 
  • #12
Nevermind I found the problem. Seems I was going way to far in my for loop like you thought. Got it all to work! Thanks for all your help!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
35K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 16 ·
Replies
16
Views
9K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K