Thread: Reading Excel Row Cells A through J

  1. #1
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    Arrow Reading Excel Row Cells A through J

    Hello again all. I would like to know how I can read from an excel cell then move over to the next column and get that data. Is it possible? below is the code i have and where im stuck.

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    void main()
    {
    
    	//variables to store in the mean
    	float mean = 0.0;
    
    	
    	//open up file
    	ifstream inFile;
    	inFile.open("gene.csv", ios:in);
    
    
    	//verify open was sucessful
    	if(!inFile.fail())	
    	{
    
    	//read cell a1 through j1
    		inFile >> Row 1 cell A through Row 1 cell J //i know this is wrong

  2. #2
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    okay forget the excel

    alright this is what ive done. Ive taken the excel data which is nothing more than 2000 rows of float numbers that span across 10 columns and saved it as a .txt file. Now im not sure if it can be done but i need my program to read the .txt file from left to right instead of top to bottom. Is this possible? i have attached the txt file so you can see what im reading

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> Now im not sure if it can be done but i need my program to read the .txt file from left to right instead of top to bottom.
    I may have misunderstood the question but...
    normally we already read a file left to right in one line until a newline and then go to second line and so on and so on.
    Or r u saying you want to read it column by colum? Easily solved by reading it normally and store in 2d array and you know which one in column1, column2,...
    What's really the problem here? Reading a bunch of floats itself?
    Last edited by alphaoide; 05-19-2004 at 01:08 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    actually im reading it from left to right like you stated

  5. #5
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    i guess its just new to me but what i have to do is take the numbers in the attachment and get the mean from the first row, store it in a new file then move to the second row. should i close the file after each calculation and reopen it to move to the next line?

  6. #6
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    forgive me if im posting too much but it seems to help to speak it out and if im doing something wrong you guys and gals could show me right?

    so i open the file. read the first row and calculate the mean. store that in a variable called mean in my program. close the file.

    open a new data file and write the mean variable to it. empty out all variables. restart the process until all 2000 rows are done. close

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >should i close the file after each calculation and reopen it to move to the next line?
    No, that would just bring you back to the first line. All you have to do is read a line, calculate the mean and write it to the output file, then start reading another line and you'll probably be in the right place. In other words, read 10 floating-point values and then calculate the mean. The read another 10 and calculate the mean again. Each set of 10 values counts as a line if I understand you correctly.
    My best code is written with the delete key.

  8. #8
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    thank you very much prelude. im working on understand microarray technology and I am in the super noob stage of analysing the thousands and thousands of amounts of data bioinformatics produces.

    you have answerd my problem and i should have the code up for a test soon. one question though.. when i open the file to write the mean variable to where does my program save that file? is it where i saved my .cpp at?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when i open the file to write the mean variable to where does my program save that file?
    Unless you specify a path, the new file will usually be created in the same directory as the executable.
    My best code is written with the delete key.

  10. #10
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    only calcs the first line

    it actually works but it only calculates the first line. how can i make it read the 1st line in "healthy.txt" then display the mean and go to the 2nd line etc... and display each mean for the 2000 rows?

    btw im using visual studio c++ 6.0 if that helps.

    Code:
    //Jared Fire
    //Find the Mean for the Gene
    
    #include<iostream.h>
    #include<fstream.h>
    
    void main()
    {
    
    	//might need variables to store in the mean
    	float mean = 0.0;
    	float cell1= 0.0;
    	float cell2= 0.0;
    	float cell3= 0.0;
    	float cell4= 0.0;
    	float cell5= 0.0;
    	float cell6= 0.0;
    	float cell7= 0.0;
    	float cell8= 0.0;
    	float cell9= 0.0;
    	float cell10= 0.0;
    
    	//begin for loop
    	for(short i=0;i<=2000;i++){
    		i++;
    
    	//open up file
    	ifstream inFile;
    	inFile.open("healthy.txt");
    
    
    	//verify open was sucessful
    	if(!inFile.fail())	//ifopen did not fail
    	{
    		//read cell a1 through j1
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
    >> cell9 >> cell10;
    
            //calculate the mean for ROW and place in mean variable
    		mean = (cell1+cell2+cell3+cell4+cell5+cell6+cell7+cell8+cell9+cell10)/10;
    	}
    
    	//open up file to write data to
    	ofstream outFile;
    	outFile.open("mean.txt",ios::app);
    
    
    	//verify open sucessful
    	if(!outFile.fail())//if open did not fail
    	{
    
    		//write the mean variable into cell
    		cout << mean << endl;
    		mean = 0.0;
    	}
    }//endforloop
    
    
    
    }//end main

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You want to open your file outside of the loop. If you keep opening it for each iteration of the loop then you'll only process the first 10 values 2000 times.
    My best code is written with the delete key.

  12. #12
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    so i should just take out the for loop? maybe?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so i should just take out the for loop?
    Yes.

    >maybe?
    Definitely.
    My best code is written with the delete key.

  14. #14
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    then eof it right

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >then eof it right
    Something like that, yes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with code modification
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 10:33 PM
  2. Merging the cells of excel from vc++
    By yongzai in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2006, 03:42 AM
  3. excel spreadsheets / blank cells
    By xddxogm3 in forum Tech Board
    Replies: 3
    Last Post: 06-14-2005, 12:10 PM
  4. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  5. Q: Simpliest way to use Excel file?
    By cDir in forum Windows Programming
    Replies: 10
    Last Post: 02-01-2002, 03:21 PM