1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Help for writnig C++ program

Discussion in 'Mac Programming' started by sitha, Jul 13, 2006.

  1. macrumors newbie

    #1
    Hi,
    I am new to C++ program, Pls help me for following probelm.
    I have a text file as following,
    DYNAMIC ANALYSIS
    次元数 ケース 内部加振 活荷重
    3 1 0 0
    KACC KDIS KSTS KSTN KSSR KJOT KNOD KRAT KSEC KMSS KWPR
    7 7 14 14 0 14 1 0 0 1 0
    FREQ GFREQ EXMAX
    0.0 2.00 1.0D-6
    VISCOUS BOUNDARY INFORMATION
    3 2.00
    77 60 11 66
    0 0
    加速度時刻歴出力節点番号(KACC=0.OR.>NTJの場合,削除)
    1080 1254 1484 1549 1563 1592 1608
    JOINT挙動出力要素番号(KJOT=0.OR.>JOINTの場合,削除)
    7 8 20 22 117 118 127
    128 129 130 133 134 139 140
    NODES
    1 -101.582 -234.742 280.000 3
    2 -76.838 -234.742 280.000 3
    3 -54.479 -234.742 280.000 3
    4 -34.276 -234.742 280.000 3
    5 -16.020 -234.742 280.000 3
    6 0.476 -234.742 280.000 3
    7 19.063 -234.742 280.000 3
    8 37.649 -234.742 280.000 3
    9 72.755 -234.742 280.000 3
    10 112.781 -234.742 280.000 3
    11 158.418 -234.742 280.000 3

    Now I want to extract only nodes from this file and put them into another text file as follows,

    101.582 -234.742 280.000
    -76.838 -234.742 280.000
    -54.479 -234.742 280.000
    -34.276 -234.742 280.000
    -16.020 -234.742 280.000
    0.476 -234.742 280.000
    19.063 -234.742 280.000
    37.649 -234.742 280.000
    72.755 -234.742 280.000
    112.781 -234.742 280.000
    158.418 -234.742 280.000
    I should omit all the setences and other details, I only need coordinates of nodes in another text file. Pls help me how to write a program.

    Regards,
    Sitha.
     
  2. macrumors regular

    nsutt22

    #2
    What knowledge in C++ do you have? You want to use fstream. ifstream for that matter. and then go from there?

    #include ifstream;

    ifstream inFile;

    inFile >> etc... etc...
     
  3. macrumors 68000

    Soulstorm

    #3
    You will need to use <fstream> Read your books to learn how to do that. Next, you must think of a way to obtain only the nodes you need.

    Firstly, I recommend yo read the file line-by-line. For example, I would read each line and insert it into a string (include <string> for working with STL strings). Then, I would put each string into a vector (include <vector> for working with STL vectors). When importing is finished, you will have a vector that will contain a string in each position.

    At that point, you will open for access a new file, using ifstream, and you are going to write each line that doesn't contain the characters you need into it. I recommend looking at this post to find some examples. Other ways exist, though.
     
  4. Moderator

    robbieduncan

    Staff Member

    #4
    Text processing is probably easier in Perl than C++. Is there a particular reason to use C++?
     
  5. macrumors 68000

    Josh

    #5
    You could either use vectors (better) or arrays to do this (though many more options exist).

    Here's a quick example of creating some arrays (to keep it simple), reading in your info, and then displaying the info you want.

    I used the standard namespace to make things more readable.

    You'd have to adjust this to make it print to a new file rather than on screen, and it has not been tested so I cannot gaurantee it works - but it should.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const int NODES = 50;                       // number of nodes expected
    
    int main()
    { string filename;
      fstream nodes_file;
      float col2[NODES], col3[NODES], col4[NODES];  // stores the data you need
      string col1[NODES], col5[NODES];  // col1 and col5 trap uneeded info
      int i, good_nodes;
    
    // set good_nodes to 0
    
      good_nodes = 0;
    
    // set dummy arrays to have no values
     
      for(i=0;i<NODES;i++)
        { col1[i] = "null";
          col5[i] = "null";
        }
    
    // begin and prompt for name
    
      cout << "Please enter the file name: ";
      cin >> filename;
      
    // open the file
    
      nodes_file.open(filename.data(),ios::in);
      
    // read the file into the arrays
    
      while(!nodes_file.eof() && !nodes_file.fail())
        { for(i=0;i<NODES;i++)
            { nodes_file >> col1[i] >> col2[i] >> col3[i]
                         >> col4[i] >> col5[i];
            }    
        }
    
    // close the file
    
       nodes_file.close();
    
    // count number of non-null entries
    
      for(i=0;i<NODES && col1[i] != "null";i++)
        good_nodes++;
    
    // print nodes to screen
    
      cout << fixed << setprecision(3);
      
      for(i=0;i<good_nodes;i++)
        { cout >> col2[i] >> col3[i] >> col4[i] >> endl;
        }
    
    return 0;
    }
    
     
  6. macrumors newbie

    #6
    it should work fine(at least works on my machine)

    hope it helps.:)
    Code:
    #include <fstream>
    #include <iostream>
    #include <String>
    #include <sstream>
    
    using namespace std;
    
    void main()
    {
    	char filename[15];
    	bool found=false;
    	string buff;
    	cout<<"Enter your file name: ";
    	//save the file name
    	cin>>filename;                                         
    	// open the input file
    	ifstream input(filename);                               
    	if(!input)
    	{
    		cout<<"can't open the file"<<endl;	
    	}
    	else
    	{
    		// open a file for output
    		ofstream output;                                    
    		// output file name
    		output.open("nodes.txt");                           
    		while(!input.eof())
    		{
    			getline(input,buff);
    			// if the line is read as NODES
    			if (buff.compare("NODES")==0)              
    			{
    				// start to read and output the following lines
    				while(!input.eof())                   
    				{
    					getline(input,buff);
    					istringstream tmp(buff);
    					// format the output
    					string first,second,third,fourth,fifth;              
    					tmp>>first>>second>>third>>fourth>>fifth;
    					output<<second<<" "<<third<<" "<<fourth<<endl;
    				}
    				found=true;
    				cout<<"Nodes found and outputed"<<endl;
    				break;
    			}
    			
    		}
    		output.close();
    	}
    	if (found==false)
    		cout<<"No node found in the file"<<endl;
    	input.close();
    	system("pause");
    }
     
  7. macrumors 65816

    munkees

    #7

    nice code layout, this is how C is meant to be layed out as was the code from kerni Ritchie the desginers of C. In some of there books it set out like

    void function() {


    if ( ...... ) {
    }

    }

    but should be :-

    void function()
    {

    if( .... )
    {
    }
    }

    much easier to read. I see alot of people have done the first which was put into books to save space.
     

Share This Page