Thread: getline??? how do I use something like it for an integer

  1. #16
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    How can I make
    Code:
    infile.getline(element[i].symbol, 3, ','); //get data until new line
    infile.getline(element[i].name, 15, ',');
    infile.getline(dummybuffer, 5, ',');	
    element[i].numb = atoi(dummybuffer);
    infile.getline(dummybuffer, 10, ',');	
    element[i].mass = atof(dummybuffer);
    Go character by character?

    Code:
    	for (i=0; (!infile.eof()) && (i <= 112); i++)
    	{
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			infile >> ch;
    			element[i].symbol[0] = ch;	
    			infile >> ch;
    			element[i].symbol[1] = ch;	
    		}
    	}
    Here's my entire code:
    Code:
    #include <fstream.h> //header for cout and cin
    #include <string.h> // header for strings
    #include <windows.h> // required for system("cls")
    
    struct chem
    {
    	char symbol[3];
    	char name[15];
    	int numb;
    	double mass;
    };
    
    main()
    {
    	ifstream infile;
    
    	int i = 0;
    	int j = 0;
    
    	char dummybuffer[26];
    	char ch;
    	
    	chem element[113];
    
    	infile.open("CHEM.DAT",ios::in);
    
    	if (!infile) {cout << "ERROR" << endl << endl; return 1;}
    	
    	for (i=0; (!infile.eof()) && (i <= 112); i++)
    	{
    		infile >> ch;
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].symbol[0] = ch;	
    			infile >> ch;
    			element[i].symbol[1] = ch;	
    			infile >> ch;
    		}
    		
    		j=0;
    		infile >> ch;
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].name[j] = ch;	
    			infile >> ch;
    			j++;
    		}
    		
    		j=0;
    		infile >> ch;
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j] = ch;	
    			infile >> ch;
    			j++;
    		}
    		element[i].numb = atoi(dummybuffer);
    
    		j=0;
    		infile >> ch;
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j] = ch;	
    			infile >> ch;
    			j++;
    		}
    		element[i].mass = atof(dummybuffer);
    
    		/*infile.getline(element[i].symbol, 3, ','); //get data until new line
    		infile.getline(element[i].name, 15, ',');
    		infile.getline(dummybuffer, 5, ',');	
    		element[i].numb = atoi(dummybuffer);
    		infile.getline(dummybuffer, 10, ',');	
    		element[i].mass = atof(dummybuffer);	*/
    
    	}
    
    	infile.close();
    
    	for (i=0; i<112; i++)
    	{
    		cout << element[i].symbol << "," << element[i].name << "," << element[i].numb << "," << element[i].mass << endl;
    	}
    
    	return 0;
    }
    }
    I'm not getting the output I want
    Last edited by Trauts; 10-31-2002 at 10:37 AM.

  2. #17
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    CHEM.DAT

    Here's my chem.dat... it might help

    Rename it to CHEM.DAT

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could use infile.get() to read in one character. Otherwise the newline gets skipped, which is messes you up.
    Code:
    	for (i=0; (!infile.eof()) && (i <= 112); i++)
    	{
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].symbol[j++] = ch;	
    			ch = infile.get();
    		}
    		element[i].symbol[j] = '\0';  //string terminator
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].name[j++] = ch;	
    			ch = infile.get();
    		}
    		element[i].name[j] = '\0';  //string terminator
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		element[i].numb = atoi(dummybuffer);
    
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		element[i].mass = atof(dummybuffer);
    Last edited by swoopy; 10-31-2002 at 02:03 PM.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you could put a comma at the end of each line. Your choice.

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And if you want to get fancy, make a function.
    Code:
    	for (i=0; (!infile.eof()) && (i <= 112); i++)
    	{
               read_token(infile,element[i].symbol);
               read_token(infile,element[i].name);
    	   element[i].numb = atoi(read_token(infile,dummybuffer));
    	   element[i].mass = atof(read_token(infile,dummybuffer));
    
    	   /*infile.getline(element[i].symbol, 3, ','); //get data until new line
    	   infile.getline(element[i].name, 15, ',');
    	   infile.getline(dummybuffer, 5, ',');	
    	   element[i].numb = atoi(dummybuffer);
    	   infile.getline(dummybuffer, 10, ',');	
    	   element[i].mass = atof(dummybuffer);	*/
    
    	}
    
    	infile.close();
    
    	for (i=0; i<112; i++)
    	{
    	   cout << element[i].symbol << "," << element[i].name << "," << element[i].numb << "," << element[i].mass << endl;
    	}
    
    	return 0;
    }
    
    char *read_token(ifstream &infile, char *token)
    {
       int j = 0;
       char ch;
    
       ch = infile.get();
       while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
       {
          token[j++] = ch;	
          ch = infile.get();
       }
       token[j] = '\0';
       return token;
    }

  6. #21
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Your adding null terminators didn't do the trick. I get lots of garbage with either of our code
    Last edited by Trauts; 10-31-2002 at 05:07 PM.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you cut and paste? It ran without any problem for me. Printed out all 112 elements. I changed quite a few things (for example, that first while-loop).

    Maybe you better paste your latest code to be sure it matches mine. You might also try shortening your loops from 112 to 5 and see what happens.

  8. #23
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Let me try your function again... yeah, it doesn't work, even with the function or reducing what it outputs.
    Last edited by Trauts; 10-31-2002 at 10:27 PM.

  9. #24
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Code:
    #include <fstream.h> //header for cout and cin
    #include <string.h> // header for strings
    #include <windows.h> // required for system("cls")
    
    struct chem
    {
    	char symbol[3];
    	char name[15];
    	int numb;
    	double mass;
    };
    
    main()
    {
    	ifstream infile;
    
    	int i = 0;
    	int j = 0;
    
    	char dummybuffer[26];
    	char ch;
    	
    	chem element[113];
    
    	infile.open("CHEM.DAT",ios::in);
    
    	if (!infile) {cout << "ERROR" << endl << endl; return 1;}
    	
    	for (i=0; (!infile.eof()) && (i <= 112); i++)
    	{
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].symbol[j++] = ch;	
    			ch = infile.get();
    		}
    		element[i].symbol[j] = '\0';  //string terminator
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			element[i].name[j++] = ch;	
    			ch = infile.get();
    		}
    		element[i].name[j] = '\0';  //string terminator
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		element[i].numb = atoi(dummybuffer);
    
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (ch != '\n') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		element[i].mass = atof(dummybuffer);
    
    
    	}
    
    	infile.close();
    
    	for (i=0; i<112; i++)
    	{
    		cout << element[i].symbol << "," << element[i].name << "," << element[i].numb << "," << element[i].mass << endl;
    	}
    
    	return 0;
    }
    What code are you using, exactly?

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I just cut and pasted your code, compiled it, and it ran perfectly on my machine!

    Try moving this:
    chem element[113];

    Above main:
    chem element[113];

    int main()
    .
    .

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It almost has the feel like your CHEM.DAT file changed. I used the one you posted.

  12. #27
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Very peculiar... now it works thanks

  13. #28
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    It works without changing it, too... weird.

    Apparently my other computer is not compiling properly.

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It sounds like your compiler doesn't realize you've changed the source file. Maybe look to see if there's a "build all" option to make sure it recompiles everything. It all depends on the compiler. Every compiler/IDE has different options.

  15. #30
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    yeah, I have that. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM