PDA

View Full Version : C++ XCode Errors




zepprock7
Oct 4, 2010, 11:37 AM
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.

#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

Anthony-MacBook-Pro-2:~ Anthony$ /Users/Anthony/Desktop/Particle\ Analysis\ Final/build/Debug/Particle\ Analysis\ Final ; exit;
0logout

[Process completed]


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

Any help would be appreciated.

Anthony



chown33
Oct 4, 2010, 12:45 PM
#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;}

The most obviously wrong code is hilited in red. There may be other problems.

lloyddean
Oct 4, 2010, 03:13 PM
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?

zepprock7
Oct 4, 2010, 04:51 PM
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:

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!

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

Thanks so much,

Anthony

Branskins
Oct 4, 2010, 05:11 PM
Why do you do cin >> array[i][j]?

I think the correct way is infile >> array[i][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.

lloyddean
Oct 4, 2010, 05:38 PM
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!

lloyddean
Oct 4, 2010, 05:55 PM
In the spirt of you first post:


#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;
}

zepprock7
Oct 5, 2010, 03:42 PM
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!

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.

zepprock7
Oct 8, 2010, 11:02 AM
All set. I ended up writing a 100 line script in R that does everything I need. Thanks again.