How to remove null characters?

This is a discussion on How to remove null characters? within the C++ Programming forums, part of the General Programming Boards category; I am going through every line of code and copying and pasting it into a new file from a previous ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    How to remove null characters?

    I am going through every line of code and copying and pasting it into a new file from a previous file because I am adding in a new column of data.

    When I go through every column, the last row has null characters or some wierd characters that are interfering with how my data looks. How can I remove it?

    Here's my code:


    Code:
    //while loop to extract data from .txt file and calculate pixel degrees
      while(getline(datain, line1))
        {
    	  
          istringstream datstream(line1, istringstream::in);
         
          datstream >> dum1[0] >> dum1[1] >> dum1[2] >> dum1[3] >> dum1[4] >> dum1[5] ;
          datastore[i][0] = strtod(dum1[0].c_str(), &pEnd); //lat 
          datastore[i][1] = strtod(dum1[1].c_str(), &pEnd); //long
    	 
    
    	 
          latpix[i] = (laurentides.lat-datastore[i][0])/(laurentides.x_pixsize);
          lonpix[i] = (datastore[i][1]- laurentides.lon)/(laurentides.y_pixsize );
    	
    	  
    
          //offset here calculates the  number of pixels that the data occupies in the actual binary file and points to it 
          offset = latpix[i]*laurentides.numsamples+lonpix[i];
          
          //here were getting the size of the data in memory
          offset = offset*sizeof(float);
          
          // cout<<"reading file at "<< offset <<endl;
    
          is.seekg (offset, ios::beg);   
          is.read ((char *) &value[i], sizeof(double));
         
          //THIS IS WHERE I RUN INTO TROUBLE I THINK......BUT DONT KNOW HOW TOFIX
            
          asciiwrite << dum1[0] << "     " <<  dum1[1] << "     " << dum1[2] << "     " << dum1[3] << "     "  <<  dum1[4] << "     " << dum1[5] << "     " << value[i] << endl;
    
          if( i == 9)
           break;
    	   
          i++;
    	
        }
    OUTPUT:

    Code:
    46.710014     -71.259471     44.489597     -1.485428     -0.059414     6.298206\     -26.1162
    46.710042     -71.259701     44.635906     -1.841427     -0.475201     3.920475\     -31.8444
    46.710211     -71.259656     44.435902     -1.663170     -0.178196     8.078243\     -23.5934
    46.710182     -71.259423     44.253120     -1.307129     0.297077     9.149914\     -16.6559
    46.710153     -71.259189     44.764111     -1.723507     -0.118870     7.547733\     -21.2177
    46.710124     -71.258954     44.675690     -1.783405     -0.237782     6.836391\     -22.4409
    46.710095     -71.258722     44.358482     -1.248703     0.356777     11.476242\     -20.1352
    46.710066     -71.258488     44.750996     -1.427460     -0.059471     8.624260\     -29.1568
    46.710037     -71.258255     44.537296     -1.427830     -0.178478     4.164490\     -31.9196
    46.710008     -71.258021     44.579990     -1.606709     -0.297535     4.165527}     -29.0219
    I want to remove "\" this and other weird characters that may pop up. this was a test run of a few lines. I don't know how though. I tried using a space width limiter but that didn't work.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    What are the declarations of dum1, datastore and value?

    Incidentally, I suggest formatting your code as:
    Code:
    //while loop to extract data from .txt file and calculate pixel degrees
    while (i < 10 && getline(datain, line1))
    {
        istringstream datstream(line1, istringstream::in);
    
        datstream >> dum1[0] >> dum1[1] >> dum1[2] >> dum1[3] >> dum1[4] >> dum1[5];
        datastore[i][0] = strtod(dum1[0].c_str(), &pEnd); //lat
        datastore[i][1] = strtod(dum1[1].c_str(), &pEnd); //long
    
        latpix[i] = (laurentides.lat - datastore[i][0]) / laurentides.x_pixsize;
        lonpix[i] = (datastore[i][1] - laurentides.lon) / laurentides.y_pixsize;
    
        //offset is the number of pixels that the data occupies in the actual binary file
        offset = latpix[i] * laurentides.numsamples + lonpix[i];
    
        //here we're getting the size of the data in memory
        offset *= sizeof(float);
    
        // cout<<"reading file at "<< offset <<endl;
    
        is.seekg(offset, ios::beg);
        is.read((char *) &value[i], sizeof(double));
    
        //THIS IS WHERE I RUN INTO TROUBLE I THINK......BUT DONT KNOW HOW TOFIX
    
        asciiwrite << dum1[0] << "     "
                   << dum1[1] << "     "
                   << dum1[2] << "     "
                   << dum1[3] << "     "
                   << dum1[4] << "     "
                   << dum1[5] << "     "
                   << value[i] << endl;
    
        i++;
    }
    Also, is this file for output or storage? If it is for storage, a lightweight relational database system like SQLite may be better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Thanks!

    Declarations:

    Code:
    double datastore[11][1];
    string dum1[6];
     float  value[11];
    This file would be to store the output lol. SQLite would probably work better but i'm supposed to do it in C++ :/. Any ideas on how I can remove that doggone "/" at the end of each line of the file I copy from?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    The declaration of datastore looks wrong; perhaps it should be:
    Code:
    double datastore[11][1];
    Since value[i] is a float, when reading into it, you should be using sizeof(float), not sizeof(double).

    Quote Originally Posted by Blah937
    SQLite would probably work better but i'm supposed to do it in C++
    SQLite's API is valid C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    There is also a C++ wrapper for SQLite. SQLite++ or something like that, in case you're interested.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove ASCII and Newline Characters
    By krouthk in forum C Programming
    Replies: 3
    Last Post: 01-12-2010, 11:15 AM
  2. Replies: 4
    Last Post: 10-29-2008, 03:22 AM
  3. How do I save null characters to a file?
    By Nate879 in forum C Programming
    Replies: 3
    Last Post: 05-13-2008, 03:07 PM
  4. how can i remove the special characters
    By cnu_sree in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 03:42 AM
  5. Question on how to remove all non-characters in string
    By learning C++ in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2004, 01:11 PM

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