having problem using ifstream to read data from text file

Discussion in 'Mac Programming' started by bamdad, Dec 20, 2009.

  1. bamdad macrumors newbie

    Joined:
    Aug 8, 2009
    #1
    Hi i have been tryin to read a gmsh text file which indicates integers, floats and some strings. the code has been posted below. i have also tried compiling and running some codes from tutorials and other sites but they share the same problem with my code. and the problem is that the .txt file opens fine but i keep getting reading error no matter what i do.

    HTML:
    #include <iostream>
    #include <fstream>
    #include <math.h>
    #include <string>
    #include "auxiliary_functions.h"
    #include "math_functions.h"
    #include "data_structure.h"
    
    using namespace std;
    
    int main () {
    	
    	const int max_num_char_in_line = 512;
    	std::cout << "This commandline tool has been designed to solve the 2D pure advection problem presented as the term project to the Finite Element Method course by Dr. Manzari. The code has been written by	Bamdad Hosseini 85110129. The code has been written using apples Xcode 3.1 and compiled under  gcc 4.2 and LLVM gcc 4.2 compilers. The problem is solved on a unit square using linear triangular elements generated by Gmsh 2.4.2. \n please push the enter if you wish to run this program." << endl;
    	//std::cin.get();
    	std::cout << endl;
    	//std::cout << "please enter the address of the Gmsh mesh file:" << endl;
    	string mesh_file_address;
    	//std::cin >> mesh_file_address;
    	//std::cin.get();
    	mesh_file_address = "/Users/bamdadhosseini/Desktop/mesh/FEMmesh.txt";
    	
    	int number_of_nd_ele[2];
    	node *global_nodes;
    	element *global_elements;
    	
    	int nd_ele_line[2];
    	float x;
    	ifstream myfile;
    	myfile.open("/Users/bamdadhosseini/Desktop/test.txt");
    	if (myfile.is_open())
    		std::cout << "file is open" << endl;
    	myfile.clear();
    	myfile.seekg(0);
    	while (!myfile.eof()) {
    		myfile >> x;
    		if (myfile.fail()) {
    			std::cout<< "read error" << endl;
    			exit(1);
    		}
    		std::cout << x << endl;
    	}
    	myfile.close();
    }
    this results in myfile.fail() to return a true value for the first iteration. i added the seekg() just to make sure i will read from the beginning but the code doesnt work without it. any how the text file that i want to read is simply

    HTML:
    1 2 32 445 6 7
    123 435 65 7
    
    and i have no idea what could be wrong with my code :( any help would be greatly appreciated
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  3. bamdad thread starter macrumors newbie

    Joined:
    Aug 8, 2009
    #3
    thanks but i still have wierd problem :(

    hi tnx for the script it works fine and it even works on my text files but you know what the problem is? surprisingly i even copied your script into my own project and ran it without any additional headers or anything i face the same problem again. please find attached to this post, my entire project up to this point. i have commented out all the extra lines and restricted the main program to run your piece of code. could it be that im building this with the wrong options in my xcode compiler?
     

    Attached Files:

  4. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #4
    Code:
    GCC_PREPROCESSOR_DEFINITIONS = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1
    I took out those preprocessor macros from your target settings, and it started working. :cool:
     
  5. bamdad thread starter macrumors newbie

    Joined:
    Aug 8, 2009
    #5
    greate

    thanks mate, so one little favour, i dont know anything about these macros. could you tell me a little more about this problem?
     
  6. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #6
    I don't know anything about them either (other than the obvious detail that they turn on the debug versions of the C++ libraries). I saw them in your settings and took them out.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    I know I had modified my project templates to fix this 'problem', but I thought there had been an Xcode update that had taken care of it as well.

    Anyone, am I mistaken?

    I'm on Xcode 3.2.1 what version of Xcode are you using?
     
  9. bamdad thread starter macrumors newbie

    Joined:
    Aug 8, 2009
    #9
    im 3.1.3 havent had time for the update yet
     

Share This Page