reading and spliting a string

This is a discussion on reading and spliting a string within the C++ Programming forums, part of the General Programming Boards category; hi all I have *.dat file containg a number of floats seperated by ',' i.e. in csv format.. I have ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    reading and spliting a string

    hi all
    I have *.dat file containg a number of floats seperated by ',' i.e. in csv format..
    I have to read all these floats and send them to an array.
    I'm making it on VC++, so plz also tell me the header files needed if you are using regex or string split. I'm getting compiling errors.
    thanks
    nj

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > I'm getting compiling errors.
    How about posting the code you have so far?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    3
    i'm using this code...it's working but giving runtime failure..."stack around 'par' is correpted..
    can somebudy tell me it's solution.
    thanks
    nj
    Code:
    #include "stdafx.h"
    #include  <fstream>
    #include  <iostream>
    #include <string>
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char buffer[100];
    	char* st;
    	float par[30][4],st1;
    	int i=0, j;
        ifstream infile("test.dat");
    
    	while(infile) 
    	{ 
    	  j=0;
          infile.getline(buffer,100);
    	  st = strtok(buffer,",");
             while (st != NULL)
    	     {
    		  st1=atof(st);
              par[i][j]=st1;
    		  //cout << par[i][j] << "\t";
    		  j++;
    	      st = strtok(NULL,",");
    	      //cout <<"\t" ;
    	     }
          i++;
    	}
    
    	for(int a=0; a<30;a++)
    	{
    		for(int b=0; b<4; b++)
    	   {
    	   cout << par[a][b] << "\t";
    	   }
           cout << "\n" ; 
    	}
     	return 0;
    }
    Last edited by Salem; 06-24-2004 at 06:30 AM. Reason: tagging the code

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That looks pretty dangerous if the data file doesn't have 30 lines with 4 items each. You need to make sure that i and j don't exceed the boundaries of the array when inputting your data.
    My best code is written with the delete key.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    A couple of points
    1) change your editor to use only spaces for indentation. Random mixes of spaces and tabs may look OK on your screen, but generally look a mess when viewed everywhere else.

    2) Don't forget the code tags when you post. See the "read this first" at the top of each forum.

    3) st and st1 are really horrible names. Tells me nothing about what you are doing

    4) Your outermost loop should be
    while ( infile.getline(buffer,100) )

    5) You may as well save a temporary with
    par[i][j]=atof(st);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    3
    hi
    thanks a lot.. the error was due to voilation of array size.. now it's working well...
    i'll take care of rest things nxt time..
    bye

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