Reading a file into an INT array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steel546
    New Member
    • Mar 2009
    • 25

    Reading a file into an INT array?

    So I'm trying to read a file into an INT array with no luck. I don't want to use a char array because I'm actually performing math operations. Any help would be GREATLY appreciated. I'll post the section I need.

    And I know there's some conversion warnings, but those are the least of my worries. The text file is simply numbers with spaces, example:

    37 181 201 89 64


    Code:
    int decrypt() {
    
    		ifstream in("encrypt.txt");
    
    		// Test to see if the file will open.
    		if(!in){
    			cout << "Cannot open file.";
                            return 1;
    		} //end if
    		
    		int arr1[160];
    		int count1=1;
    
    		for(int g=0; g<count1; g++){  // Not sure what to do here?
    			in.getline(arr1[g], ' ');
    		}
    
    		in.close();
    
    		for(int k1 = 0; k1 < count1; k1++){
    
    			double m, m1;
    			m = arr1[k1];
    			m1 = pow (m,d);
    			double m2 =  fmod(m1,n);
    			arr1[k1] = m2;
    			//cout << a[k] << endl;
    		} //end for
    
    		for(int ij=0; ij < count1; ij++){
    			cout << arr1[ij] << " ";
    		}
    
    
    	}; //end decrypt
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    a little research suggests that fscanf()may be a better bet than in.getline()

    Comment

    • Steel546
      New Member
      • Mar 2009
      • 25

      #3
      It's alright, I guess I can close this. I had to write some TCP socket layer stuff in Java, so I just completely re-wrote everything in Java (nightmare).

      Thanks for the suggestion, I'll look into that the next time.

      Comment

      Working...