C++ XCode Errors

Discussion in 'Mac Programming' started by zepprock7, Oct 4, 2010.

  1. macrumors newbie

    Joined:
    May 14, 2008
    #1
    Hey everyone,

    So I took a programming class 4 years ago in college and thought I'd never have to use it again. Well turns out writing a C++ program for my job would be very helpful.

    I'm trying to import an array from a text file. I remember seeing in a different thread that I need to delete a couple build files but I can't remember where I saw it or what to delete.

    Code:
    #include<iostream> 
    using namespace std;
    #define MAX 3 //maximum for array
    #include<fstream>
    
    int main(void) //start main function
    {int i, j; //declare integers
    	
    	double array[MAX][MAX]; //declare array
    
    	ifstream infile;  //pointer with permission to read from a file
    	infile.open("/users/anthony/desktop/123.txt"); //choose infile
    	
    	if(!infile) //check infile
    	{	cout<<"Could not find data file to open. Check file name or location."<<endl; //check
    		return 0;}
    	
    	
    	//read in array
    	
    	{
    		for (i=0;i<3;i++)
    			for (j=3;j<MAX;j++)
    			{
    				cin>>array[i][j];
    			}}
    	
    	for (i=0;i<3;i++){
    		for (j=3;j<MAX;j++)
    		{
    			cout<<array[i][j];
    		}}
    		
    	return 0;}
    
    is my code. It compiles with no errors but when I run it, I do not see the array and the only output I get is

    The array is [1 2 3; 4 5 6; 7 8 9].

    Any help would be appreciated.

    Anthony
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    The most obviously wrong code is hilited in red. There may be other problems.
     
  3. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    You open a filestream then read from cin?

    Is the source data in the file in text form or is it really just binary doubles?

    If binary doubles you need use 'read' instead of the file extractor operator. Also what byte order (endianess) is the binary double stored in the file? You may need to do endian swapping on the data before it can be used!

    EDIT: Would you mind posting a sample "123.txt" file?
     
  4. thread starter macrumors newbie

    Joined:
    May 14, 2008
    #4
    Here it is:

    123.txt is a basic array I was using. I also attached the type of file that I will actually be using:

    I'm sorry, I'm not sure what this means. Its a simple text file with a few decimal places.

    Thanks so much,

    Anthony
     

    Attached Files:

    • 123.txt
      File size:
      236 bytes
      Views:
      24
    • w2.txt
      File size:
      30.2 KB
      Views:
      29
  5. macrumors 6502a

    Joined:
    Dec 8, 2008
    #5
    Why do you do cin >> array[j]?

    I think the correct way is infile >> array[j]. After all, you are opening the file to be read and then extracting that data. I don't think cin is associated with the file unless you use indirection via the terminal ( > to send text file data as the input for the program).

    Does the program wait for input from you?

    Also, isn't a while loop better for reading in data from a line? Your condition can test for the end of line ('\n'). If I remember, for loops are good for when you know how many items you are reading while while loops are good for when you don't know.
     
  6. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Some consistency would be nice. You post 2 distinctly different text files.

    123.txt contains 3 lines of ASCII text the first of which consists of alpha characters and the remaining 2 are integers.

    The second file w2.txt consists of ASCII text consisting of numbered lines, 0 - 259, of 18 floating point values.

    Neither of these correspond to your first post of 3 x3 floats.

    Please provide a better description of what you wish to do. And tell me whuch of the 2 files is the real data!
     
  7. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    In the spirt of you first post:

    Code:
    #include<fstream>
    #include<iostream> 
    
    #define ROWS    3
    #define COLS    3
    
    int main(int argc, const char* argv[])
    {
        using std::cout;
        using std::endl;
        using std::ifstream;
        
        double array[ROWS][COLS];
        
    //  ifstream ifs("/Users/anthony/Desktop/123.txt");
        ifstream ifs("./123.txt");
        if ( ! ifs.is_open() )
        {
            cout << "Could not find data file to open. Check file name or location." << endl;
            return EXIT_FAILURE;
        }
        
        for ( int row = 0; row < ROWS; row++ )
        {
            for ( int col = COLS; col < COLS; col++ )
            {
                ifs >> array[row][col];
            }
        }
        
        for ( int row = 0; row < ROWS; row++ )
        {
            for ( int col = 3; col < COLS; col++)
            {
                cout << array[row][col];
            }
        }
        
        return EXIT_SUCCESS;
    }
    
     
  8. thread starter macrumors newbie

    Joined:
    May 14, 2008
    #8
    I appreciate the help! Basically, this is a scan of an image that I took in Image J. I wrote a macro that will output the position along the image (column 1) and the intensity of the signal in each of the other columns. My final goal is to import an NxM array into a C++ program, perform some calculations on the array (subtract noise), and then output a new file that I can import into excel and plot. I can do this in excel but when I'm dealing with 350 rows and 60 columns, staring at an excel spreadsheet gets annoying. The real data is is the w2.txt file, but the arrays I'll be importing can vary in size.

    Again, thanks so much for looking this over.
     
  9. thread starter macrumors newbie

    Joined:
    May 14, 2008
    #9
    All set. I ended up writing a 100 line script in R that does everything I need. Thanks again.
     

Share This Page