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

  • Thread starter TruthOasis
  • Start date
  • Tags
    Array File
In summary, the individual is struggling with incorporating the use of scanf() into their program to read data from a text file and store it into arrays. They are also unsure of how to properly handle multi-dimensional arrays and how to use pointers. With the help of another person, they were able to successfully read and store the data from the file into arrays.
  • #1
TruthOasis
7
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
  • #2
take a look at scanf()
 
  • #3
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) ?
 
  • #4
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.
 
  • #5
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;
}
 
  • #6
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 becuase 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.
 
  • #7
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);
 
  • #8
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()
 
  • #9
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!
 

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

How do I read a text file into an array in C?

To read a text file into an array in C, you will need to follow these steps:

  • Open the text file using the fopen() function and store the file pointer in a variable.
  • Use the fscanf() function to read the contents of the file and store them in the array.
  • Close the file using the fclose() function.

Can I specify the size of the array when reading from a text file in C?

Yes, you can specify the size of the array when reading from a text file in C. However, you will need to know the number of elements in the file beforehand or use dynamic memory allocation to create an array of appropriate size.

How do I handle errors when reading a text file into an array in C?

To handle errors when reading a text file into an array in C, you can use the feof() function to check if the end of file has been reached and the ferror() function to check for any errors that may have occurred during the reading process.

What is the best way to convert a text file to an array in C?

The best way to convert a text file to an array in C depends on the specific requirements of your program. If you know the number of elements in the file, you can use a fixed-size array. Otherwise, you can use dynamic memory allocation to create an array of appropriate size. You can also use functions like fread() or fgets() to read the file contents directly into the array.

Can I read a text file with non-numeric data into an array in C?

Yes, you can read a text file with non-numeric data into an array in C. However, you will need to use the appropriate format specifier in the fscanf() function to read the data correctly. For example, you can use %s to read strings, %c to read characters, or %f to read floating-point numbers.

Similar threads

Replies
4
Views
2K
Replies
11
Views
34K
Replies
13
Views
2K
Replies
4
Views
11K
Replies
10
Views
3K
Replies
16
Views
8K
Replies
2
Views
3K
Back
Top