VC 2008 чтение из файла

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by zaki, 10 Mar 2011.

  1. zaki

    zaki New Member

    Joined:
    12 Nov 2010
    Messages:
    13
    Likes Received:
    0
    Reputations:
    0
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main(){
    	ofstream f("file.txt");
    	int i;
    	for (i=0; i<10; ++i)
    	{
    		int v;
    		f >> v;
    		cout << v << endl;
    	}
    	system ("pause")
    		return 0;
    }
    Должно считывать 10 строк из тхт и выводить, при компиляции следушие ошибки
    Code:
    
    [code]
    1>h:\troyanzxc\zxc\zxc\1.cpp(10) : error C2784: std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &): не удалось вывести аргумент шаблон для "std::basic_istream<char,_Traits> &" из "std::ofstream"
    1>        c:\program files\microsoft visual studio 9.0\vc\include\istream(1021): см. объявление 'std::operator >>'
    1>h:\troyanzxc\zxc\zxc\1.cpp(10) : error C2784: std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &): не удалось вывести аргумент шаблон для "std::basic_istream<char,_Traits> &" из "std::ofstream"
    

    хз что за ошибки...
     
  2. X-rus

    X-rus Member

    Joined:
    22 Dec 2010
    Messages:
    88
    Likes Received:
    22
    Reputations:
    4
    Ты используешь поток вывода в файл. Используй ifstream.
     
  3. AlexTheC0d3r

    AlexTheC0d3r Elder - Старейшина

    Joined:
    25 Jul 2008
    Messages:
    388
    Likes Received:
    179
    Reputations:
    18
    мдааа... новое поколение кодеров....
    В добавок, почему 10 строк (!!!)?
     
  4. zaki

    zaki New Member

    Joined:
    12 Nov 2010
    Messages:
    13
    Likes Received:
    0
    Reputations:
    0
    Так захотел, спасибо я почти разобрался. Вот только почему то чувствует пробел, и сразу перебегает на новую строку
     
  5. t-s

    t-s New Member

    Joined:
    5 Mar 2011
    Messages:
    30
    Likes Received:
    4
    Reputations:
    0
    Можешь считать в вектор если надо построчно с помощь geline()

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    ifstream File("test.txt");	
    string Line;
    
    vector <string>test;
    while(!File.eof())
    {
    	getline (File,Line);
    	test.push_back(Line);
    	        
    }
    
    
    for(vector<string>::size_type index = 0; index < 10; index ++)
    
       {
    	
    	cout << test[index] << endl;
    	
       }
    
    cin.get();
    
    	return 0;
    
    }
    
    
     
  6. WNZRS

    WNZRS Member

    Joined:
    3 Sep 2009
    Messages:
    294
    Likes Received:
    52
    Reputations:
    1
    А как он тебе пробел в int должен записать? В итоге у тебя читает не 10 строк, а 10 чисел.