Parse the line to extract each of the twelve data items about the element. Save each of the twelve items in the next available array element. See the data format description below. Hint: Use strtok( ) as much as possible.
Parse the line to extract each of the twelve data items about the element. Save each of the twelve items in the next available array element. See the data format description below. Hint: Use strtok( ) as much as possible.
Hint: Don't bother with strtok at all.
1) FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
2) Consider using sscanf to split your line up.
Let's see your attempt. Oh, while you're at it:
Originally Posted by kermi3
Read and follow the above instructions, and try your post (in the same thread) again.
Quzah.
Hope is the first step on the road to disappointment.
sorry my bad,
i have an assignment tha involves opening a file and reading and printing different lines of the file, there is this one part that im having problems with, im using strings and arrays
***Parse the line to extract each of the twelve data items about the element. Save each of the twelve items in the next available array element. See the data format description below. Hint: Use strtok( ) as much as possible.***
here is the code i have so far. the file that i have to open is periodic.txt and is a list of all the periodic elements in alfabetical order, sofar i have printed the first and last elements but this weird suff appears in between them when i comile my programm. BTW im using visual studio c++
Code:#include<stdio.h> #include<math.h> #include<string.h> int main (){ int choice; char description[120][120]; FILE *myfile = NULL; int icount; printf("1) Read the chemical elements file\n"); printf("2) Display results\n"); printf("3) Write a new chemical elements file\n"); printf("4) Reinitialize the chemical elements array\n"); printf("5) Exit\n"); scanf("%d", &choice); if(choice==1){ myfile = fopen("periodic.txt", "r"); printf("file is now open\n"); if( myfile != NULL ){ icount = 0; }} if(choice==2){ myfile = fopen("periodic.txt", "r"); for( icount = 0; icount <111 ; icount++) { fgets( description[icount], 120, myfile ); printf( "\nFinished reading %d lements.\n", icount ); printf("%s", description[0]); printf( "%s", description[1]); printf("%s", description[109]); } } }
any help and suggestions are welcomed.
Last edited by packer; 04-17-2005 at 12:55 PM.
You need to only open the file once. If you want a seperate menu selection to open the file, consider something like this:
Now, you can do the same thing using a switch instead if you prefer:Code:...variable declarations up here, plus some I'm defining like so... int opened = 0; do { printf("...display your menu here..."); ...read your 'choice' here... if( choice == 1 ) { if( opened != 0 ) { printf("...file is already open..."); } else { myfile = fopen( ...yourfile and method... ); if( myfile != NULL ) opened = 1; } } if( choice == 2 ) { if( opened == 0 ) printf("file not open yet"); else { ...stuff here... } } ...other choices here... } while( choice != 5 )
Now on to reading the file, you could do it this way:Code:do { ...menu display, and reading into 'choice'... switch( choice ) { case 1: ...all of selection 1 goes in here... break; case 2: ...all of selection 2 goes in here... break; } }
Quzah.Code:for( icount = 0; icount < SOMENUMBER; icount++ ) { if( fgets( description[ icount ], SOMESIZE, myfile ) == NULL ) { ...display message about reaching end of file... break; /* this breaks you out of the loop */ } ...otherwise, display the contents or do whatever... }
Hope is the first step on the road to disappointment.
thanks quzah that helped for the menu selection but i still cant figure out how to do this to the file seperate the line to extract each of the twelve data items about the element. Save each of the twelve items in the next available array element. See the data format description below. Hint: Use strtok( ) as much as possible.
does this mean i have to have 12 different arrays???
Well it helps if you show a portion of what your input file looks like. Have you covered structures yet? You could create a structure to hold one line's worth of data (the 12 items), and use an array of those. Again, it will help to see what your input looks like to get an idea of how it's to be stored. As is, we don't really know anything about it, other than the fact that there are 12 somethings per line.
Quzah.
Hope is the first step on the road to disappointment.
this is what my file looks like,
we have partially covered structures, i have declared al my variables this wayCode:Element Symbol Class Atomic no Atomic wt. Electrons Actinium Ac rare_earth 89 2272.0000 2 8 18 32 18 9 2 Silver Ag transition_metal 47 107.8682 2 8 18 18 1 0 0
but the problem is i dont know ho w to copy each segment of the text line, i know i have to use strcpy but i dont know how...i know i have to put the first 13 or so characters of the first line into name, the next 3 columns should go into the symbol, but i dont know how to declare my parameters so that strcpy copies a sertain number of colums into my variable name.Code:struct Data { char name[15]; char symbol[3]; char type[25]; int number; double weight; int shell_1; int shell_2; int shell_3; int shell_4; int shell_5; int shell_6; int shell_7; };
pls help
Last edited by packer; 04-17-2005 at 09:52 PM.
Well you could always do like I suggested in my first post: use sscanf.
Quzah.Code:if( fgets( buffer, buffersize, file ) != NULL ) { success = sscanf( buf, "%s %s %s %d %f %d %d %d %d %d %d %d", mystructarray[ icount ].name, mystructarray[ icount ].symbol, mystructarray[ icount ].type, & mystructarray[ icount ].name, ...the rest of the fields... & mystructarray[ icount ].shell_7 ); if( success != 12 ) { ...error, 12 items weren't in that line and didn't scan right... ...break from the loop... } }
Hope is the first step on the road to disappointment.
but sscanf is a function for user inputs, i have to store certain parts of my file lines into the structures...what can i use ? to copy the first 15 culumns of the array into my structure array?
>> sscanf is a function for user inputs
you mixing up sscanf with scanf?
sscanf reads formated input from a string
fscanf reads formated data from a file/stream
scanf is same as fscanf, with the stream set to STDIN (thus keyboard)
maybe reread quzahs post and realize the power of *scanf
signature under construction
how then would i have to store the values using sscanf into my array of structures any help?
The same way you store it in a non pointer. You just insert the array name and indexing in there:
Naturally you either use or don't use the & depending on if the array member is a pointer or not. Also, the dot becomes an arrow if it's a pointer to a structure instead of an instance of a structure.Code:sscanf( buffer, "format", &arrayname[ index ].structuremember, ... );
Quzah.
Hope is the first step on the road to disappointment.
i got the idea of sscanf, but the question is...i have my first sting, (the frist line of the file) it has 12 columns for the name and 3 for the symbol and so on...how then do i copy the first 12 columns into my first variable?
I already showed you two posts ago.
Quzah.
Hope is the first step on the road to disappointment.