throwing away spaces and \n

This is a discussion on throwing away spaces and \n within the C++ Programming forums, part of the General Programming Boards category; Hey, quick question. I am reading in numbers from a file. I need to read them in one at a ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    throwing away spaces and \n

    Hey,
    quick question. I am reading in numbers from a file. I need to read them in one at a time. In the file there are spaces and endlines that I need to get rid of. I have tried several ways of doing this but none work. Any help would be appreciated.

    Here is what I have so far:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    //global variables
    ifstream infile;
    ofstream outfile;
    struct node;
    typedef node* nodeptr;
    
    struct node
    {
    	int digit;
    	char num;
    	nodeptr next;
    };
    
    //prototypes
    void readIn(nodeptr &,nodeptr &);
    int charConvert(char);//num conversion fxn
    
    
    int main()
    {
    	nodeptr start, last;
    	readIn(start,last);
    
    	return 0;
    }
    //Functions
    void readIn(nodeptr &start,nodeptr &last)
    {
    	char c,d;
    	nodeptr current;
    	char temp;
    
    infile.open("lnums.dat");
    if(!infile)
    	{
    		cout << "error" << endl;
    		exit(1);
    	}
    while(!infile.eof())
    {
    	//c=infile.get(temp);
    	c=(char)infile.get();
    	if((c==' ')&&(c=='\n'))
    	{
    		temp=c;	
    	}
    else
    {
    	d=c;
    	
    }
    cout << d;
    
    
    
    
    
    //START sorting
    	current=new node;
    	//strcpy(current->num,temp);
    	current->num=temp;
    	current->next=start;
    	start=current;
    
    	int charConvert(temp);
    }
    
    }
    int charConvert(char a)
    {
    	int x;
    	x=a-48;
    return x;
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    umm, im not really sure but try this or something like this...

    Code:
    ifstream in;
    char c;
    int buf;
    in.open("myfile.txt",ios::in); //I think... 
    while(in.get(c))
    	while(c!='\n' || c!=' ')
    	{
    		buf = atoi(c);	
    		//Todo code here
    	}
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Very cool idea. I tried it and it gave me this error:

    error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'

    the code looks like this:
    Code:
    while(!infile.eof())
    {
    	c=(char)infile.get();
    	while(c==' '|| c=='\n')
    	{
    		temp=atoi(c);	
    	}

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Just an idea off the top of my head, assuming there are never more than 1 space in a row:
    Code:
    char buffer[256]; //assuming the max number of characters
                           //on a line is 256
    std::vector<int> numbers;
    while(true)
    {
         file.getline(buffer, 256);
         if(file.eof())
              break;
    
         int pos = 0;
         for(int i = 0; i < file.gcount(); ++i)
         {
              if(buffer[i] == ' ')
              {
                   std::string temp;
                   temp.append(buffer[pos], i - pos);
                   numbers.push_back(atoi(temp.c_str()));
                   pos = i + 1;
              }
         }
    }
    Something like that, gets a line, and goes through the input pulling out individual sections separated by 1 space each.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Why are you using the get() function? The >> operator automatically disregards white space when looking for numerical input. You can just do this:
    Code:
    infile >> c;
    If you did that, you wouldn't have to worry about disregarding white space in your input file.

    Oh, and don't use global variables. That is bad. Don't do it. You should pass the ofstream and ifstream objects from main() to the functions that use them.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oops lol I was thinking of what happens when the text file includes letters and symbols too instead of just numbers Guess my way didn't do it any better either...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Originally posted by joshdick
    Why are you using the get() function? The >> operator automatically disregards white space when looking for numerical input. You can just do this:
    Code:
    infile >> c;
    If you did that, you wouldn't have to worry about disregarding white space in your input file.

    I think the file he is trying to get numbers like
    1234211523 and take them individually or pick them apart or I don't know. But if it is like this:
    Code:
    13 14 15
    16 17 18 19
    20 21 22
    23 24
    then yeah that works better. When I saw the post I thought it was slightly more akward than that.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21