Thread: how do i do this?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    how do i do this?

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

    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.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to only open the file once. If you want a seperate menu selection to open the file, consider something like this:
    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, you can do the same thing using a switch instead if you prefer:
    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;
        }
    }
    Now on to reading the file, you could do it this way:

    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...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    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???

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    this is what my file looks like,


    Code:
    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
    we have partially covered structures, i have declared al my variables this way

    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;
    	};
    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.

    pls help
    Last edited by packer; 04-17-2005 at 09:52 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you could always do like I suggested in my first post: use sscanf.
    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...
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    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?

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    >> 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

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    how then would i have to store the values using sscanf into my array of structures any help?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you store it in a non pointer. You just insert the array name and indexing in there:
    Code:
    sscanf( buffer, "format", &arrayname[ index ].structuremember, ... );
    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.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    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?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already showed you two posts ago.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed