Thread: Reading Numbers from files...

  1. #31
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    while (!Graph.eof())
    Nope. Look at the other posts in this thread, and even your own example that you posted first.

    Graph>> Matrika[j][i]; // Matrika = matrice in which the numbers from the file are saved

    should skip commas automatically, so you should be able to remove that Graph>>buu; line.
    Doesn't work for me:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    
    	ifstream inFile("C:\\TestData\\data.txt");
    	/* data.txt:
    
    	-1,55,25,45,-1,-1,-1,-1
    	
    	*/
    
    	int num[10] = {0};
    	inFile>>num[0];
    	cout<<num[0]<<endl; //-1
    
    	inFile>>num[1];
    	cout<<num[1]<<endl; //0  ???
    
    	return 0;
    }

  2. #32
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Doesn't work for me:
    7stud, yes you're right.

    I guess a few options are:

    (1) If it was easy enough, reformat the file, replacing the commas with spaces.

    (2) Add a comma to the end of each line in the file.

    (3) Use C style I/O to read the file.

    (4) Read the file a line at a time, and parse.

  3. #33
    Registered User kotzy's Avatar
    Join Date
    Jul 2005
    Posts
    5
    thanks swoopy! now it works fine!

    here is the final code. it's altered a bit.

    Code:
    int stev;
    while (!Graf.eof()) {
    	Graf>> stev; // reads numbers
    	if (stev == -1) Matrika[j][i] = 10000; // if a value in the file is -1 (which means there is no link with another node) then write 10k into the matrix
    	else Matrika[j][i] = stev; //else just put the number in the matrix
    	i++;
    	if (i < m) {
    		Graf>> buu; //if it's not at the end of the line,read commas
    	}
    	else {
    		i = 0; // else just skip to next line!
    		j++;
    	}
    }
    EDIT: i just now saw that there are more posts on the next page. well,i guess i don't have to change anything because it is working perfectly. i have already tested it with numerous matrices and no errors occur. Oh,and the numbers HAVE to be separated with commas. Thanks again!
    Last edited by kotzy; 07-31-2005 at 03:19 AM.

  4. #34
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    while (!Graf.eof())
    ...still wrong. Reaching eof causes an error flag to be set in the stream, which terminates the loop. However, other things can cause an error flag to be set in the stream before eof is encountered. If an error flag gets set in the stream before eof is encountered, no further values can be read from the stream. That means eof will never be reached, and consequently your while loop will never terminate--causing an infinite loop.
    Last edited by 7stud; 07-31-2005 at 03:48 AM.

  5. #35
    Registered User kotzy's Avatar
    Join Date
    Jul 2005
    Posts
    5
    Quote Originally Posted by 7stud
    Code:
    while (!Graf.eof())
    ...still wrong. Reaching eof causes an error flag to be set in the stream, which terminates the loop. However, other things can cause an error flag to be set in the stream before eof is encountered. If an error flag gets set in the stream before eof is encountered, no further values can be read from the stream. That means eof will never be reached, and consequently your while loop will never terminate--causing an infinite loop.
    i don't understand. in which occurence can an error flag be expected?

    i ran the program with three different files,and each produced a correct minimum spanning tree with prim's algorithm.i helped myself with debug and no errors occured. how else can i check whether i have errors in my program?

  6. #36
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    >i don't understand. in which occurence can an error flag be expected?

    just put

    Code:
    Graf >> stev
    as your loop condition.

  7. #37
    Registered User kotzy's Avatar
    Join Date
    Jul 2005
    Posts
    5
    Quote Originally Posted by dra
    >i don't understand. in which occurence can an error flag be expected?

    just put

    Code:
    Graf >> stev
    as your loop condition.
    hello!after more than an hour of fcuking with dynamic arrays i did what you said, and it worked fine. and all this time i thought my compiler has gone nuts,by not understanding the basic initialisation of 2D matrix.

    well,thank you very much!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  2. pulling numbers from files....
    By Confuzz in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2004, 07:49 PM
  3. Reading selective numbers from a file
    By supaben34 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2002, 01:56 PM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM