• Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001,

    #include "stdafx.h" 
    #include <iostream>  
    #include <fstream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iterator>
    #include <conio.h>
    
    
    int main()
    {
    	
    	
    
    	std::ofstream binaryFile("file.dat", std::ios::out | std::ios::binary);
    	std::fstream input2("input2.txt");
    	int i2 = 0;
    
    	std::vector<short> vec; // значения из файла input.txt
    	std::copy(std::istream_iterator<int>(input2), std::istream_iterator<int>(), std::back_inserter(vec));
    	std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
    
    	unsigned short m_bytesCount;
    	for (size_t i = 0; i < 3; i++)
    	{
    		m_bytesCount = 0;
    		for (size_t k = 0; k < 6; ++k)
    		{
    			if (vec[++i2] == 1)
    				m_bytesCount |= 1 << (k + 1);
    		}
    		binaryFile.write((char *)&m_bytesCount, sizeof m_bytesCount);
    	}
    
    
    	return 0;
    
    	}


    input2.txt
    1 1 1 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1

    В итоге исключение
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001, не хватает элементов в векторе. При последнем прохождении цикла, последнем значении, выскакивает исключение. Добавила в файл еще 0, работает.
    Но это же неправильно.
    Где-то я конкретно туплю(
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001, нужно было
    int i2 = 0;
    в цикле инициализировать.
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001, у меня исключение vector subscript out of range
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001
    unsigned short m_bytesCount = 0; 
    
    	for (size_t i = 0; i < 3; i++) 
    	{
    
    
    		for (size_t k = 0; k < 6; ++k)
    		{
    		
    			if (vec[i2++] == 1)
    				m_bytesCount |= 1 << i2;
    
    		
    
    		}
    
    		
    		binaryFile.write((char *)&m_bytesCount, sizeof m_bytesCount);
    	}
    		return 0;


    не то все равно
    сейчас такой результат
    60 00 60 18 60 18
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001
    я из файла в вектор записала, беру по 6 элементов из вектора
  • Записать значения дискретных каналов в бинарный файл. С++?

    @kytcenochka Автор вопроса
    res2001

    Пишу вектор из файла

    input2.txt
    0 0 0 0 1 1
    0 0 0 0 1 1
    0 0 0 0 1 1
    #include "stdafx.h" 
    #include <iostream>  
    #include <fstream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iterator>
    
    int main()
    {
    	int i2 = 0;
    	
    
    	std::ofstream binaryFile("file.dat", std::ios::out | std::ios::binary);
    
    	std::fstream input2("input2.txt");
    
    	std::vector<short> vec; 
    	std::copy(std::istream_iterator<int>(input2), std::istream_iterator<int>(), std::back_inserter(vec));
    	std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
    
    	unsigned short m_bytesCount = 0; 
    
    	for (size_t i = 0; i < 3; i++) 
    	{
    		
    
    		for (size_t k = 1; k < 6; ++k)
    		{
    
    			if (vec[++i2] == 1)
    
    			        m_bytesCount |= 1 << i2;
    			
    		}
    
    		binaryFile.write((char *)&m_bytesCount, sizeof m_bytesCount);
    	}
    
    		return 0;
    	}


    на выходе

    30 00 30 04 30 0C
    Почему только при первом прохождении цикла записывается правильное значение?
    Откуда 04 0С, строки одинаковые
  • Запись вектора в бинарный файл, циклом - C++?

    @kytcenochka Автор вопроса
    В файле input2.txt хранятся наборы битов, каждый бит может иметь одно из двух значений – 0 или 1.
    Значения каждой строки должны хранится в двух байтах.
    Строку 0 0 0 0 1 1 можно записать в два байта как 30 00 используя побитовые операторы, а именно присваивание с побитовой операцией ИЛИ |=.

    Вот в первой строке записывается правильно, а затем идет сбой

    01 00 00 00 01 00 02 00 03 00 04 00 05 00 06 00 30 00 02 00 00 00 07 00 08 00 09 00 0А 00 0В 00 0С 00 00 0С 03 00 00 00 0D 00 0E 00 0F 00 10 00 11 00 12 00 00 00
  • Запись вектора в бинарный файл, циклом - C++?

    @kytcenochka Автор вопроса
    Спасибо!
    Вопрос еще возник, добавила второй файл из которого добавляю значение в каждую строку
    input2.txt:
    0 0 0 0 1 1
    0 0 0 0 1 1
    0 0 0 0 1 1
    0 0 0 0 1 1
    0 0 0 0 1 1

    Почему только в первой строке пишет верное значение 30 00, потом уже не то

    int main()
    {
    	std::fstream input("input.txt");
    	std::fstream input2("input2.txt");
    
    	std::vector<short> vec; // значения из файла input.txt
    	std::copy(std::istream_iterator<int>(input), std::istream_iterator<int>(), std::back_inserter(vec));
    	std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
    
    	std::vector<int> vec2; // значения из файла input2.txt
    	std::copy(std::istream_iterator<int>(input2), std::istream_iterator<int>(), std::back_inserter(vec2));
    	std::copy(vec2.begin(), vec2.end(), std::ostream_iterator<int>(std::cout, " "));
    
    	std::ofstream fout("data.dat", std::ios::out | std::ios::binary);
    	const size_t columnCount = 6;
    
    	for (size_t i = 0; i < vec.size(); i += columnCount)
    	{
    		int index = i / columnCount + 1;
    		fout.write((char *)&index, sizeof(int));
    
    		for (size_t j = 0; j < columnCount; j++)
    		{
    			fout.write((char*)&vec[i + j], sizeof(short));
    		}
    
                   // Запись значений второго файла
    		unsigned short m_bytesCount = 0; // запись 1й строки в 2 байта
    
    		for (size_t k = 0; k < columnCount; k++)
    		{
    
    			if (vec2[i+k] == 1)
    				m_bytesCount |= 1 << i+k;
    		}
    		fout.write((char *)&m_bytesCount, sizeof m_bytesCount);
    	}
    	fout.close();
    	return 0;
  • Запись вектора в бинарный файл. С++ Читать бинарный файл?

    @kytcenochka Автор вопроса
    Спасибо, но все же, как так получается?
    01 00 02 00 03
  • Ошибки при заполнении вектора. С++?

    @kytcenochka Автор вопроса
    Роман, именно классов
  • Ошибки при заполнении вектора. С++?

    @kytcenochka Автор вопроса
    Роман 45 ошибок
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\string(537) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\string(537) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\string(537) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\string(537) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream &std::operator <<(std::basic_ostream &,unsigned char)' : could not deduce template argument for 'std::basic_ostream &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(930) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream &std::operator <<(std::basic_ostream &,unsigned char)' : could not deduce template argument for 'std::basic_ostream &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(930) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream &std::operator <<(std::basic_ostream &,unsigned char)' : could not deduce template argument for 'std::basic_ostream &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(930) : see declaration of 'std::operator <<'
    1>d:\test18.05\test18.05\test18.05.cpp(56) : error C2784: 'std::basic_ostream &std::operator <<(std::basic_ostream &,unsigned char)' : could not deduce template argument for 'std::basic_ostream &' from 'std::string'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(930) : see declaration of 'std::operator <<'
  • Запись файла построчно с помощью отдельных функций. С++?

    @kytcenochka Автор вопроса
    Станислав Макаров, а если там могут быть пропущенные значения?
  • Запись файла построчно с помощью отдельных функций. С++?

    @kytcenochka Автор вопроса
    Станислав Макаров,

    1>d:\test_03.05.18\test_03.05.18\test_03.05.18.cpp(22) : error C2059: syntax error : ','
    1>d:\test_03.05.18\test_03.05.18\test_03.05.18.cpp(23) : error C2059: syntax error : ','

    ругается на эти строки

    InfAnalogChanel a = (1,"ток ф А",,,"A",1.290489E-01,0,0,-32767,32767,3000.00000000,1.00000000,"P");
    InfAnalogChanel b = (2,"ток В",,,"A",5.139708E-02,0,0,-32767,32767,3000.00000000,1.00000000,"P");
  • Значения не записываются в файл. Почему? с++?

    @kytcenochka Автор вопроса
    Александр Титов,
    переделала, есть ошибки. Подскажите, пожалуйста как написать именно реализацию функции writesecondline в function1.cpp

    function.h
    class InfAnalogChanel
    { 
    public:
       int id;
       std::string chanell_id;
       std::string ph;
       std::string ccmb;
       std::string UU;
       double a;
       double b;
       double skew;
       double min;
       double max;
       double Primery;
       double Secondary;
       std::string SP;
    };
    void writesecondline (std::ostream &file, InfAnalogChanel const &iac);


    function1.cpp
    void writesecondline(std::ostream &file, InfAnalogChanel const &iac){
               ofstream file("fileConfiguration.cfg");
       
           }


    main.cpp
    InfAnalogChanel a = {1,"ток ф А",,,"A",1.290489E-01,0,0,-32767,32767,3000.00000000,1.00000000,"P"};
      InfAnalogChanel b = {2,"ток В",,,"A",5.139708E-02,0,0,-32767,32767,3000.00000000,1.00000000,"P"};
    
      ofstream file("fileConfiguration.cfg");
      writesecondline(file, a);
      writesecondline(file, b);
  • Значения не записываются в файл. Почему? с++?

    @kytcenochka Автор вопроса
    Богдан , подскажите пожалуйста
    Нужно получить файл вида:
    1,Раб МТЗ,,,0
    2,3_3,,,0
    3,8_8,,,0
    4,8_100,,,0
    5,3_8,,,0

    Написала так, не работает конечно, но суть та?
    или бред полный?
    function.h
    описываю структуру
    class InfAnalogChanel
    { 
    public:
    
       int id;
       std::string chanell_id;
       std::string ph;
       std::string ccmb;
       std::string UU;
       double a;
       double b;
       double skew;
       double min;
       double max;
       double Primery;
       double Secondary;
       std::string SP;
    
    };
    
    vector<InfAnalogChanel> s;
    
    void writesecondline (s[i].id, s[i].chanell_id, s[i].ph, s[i].ccmb, s[i].UU, s[i].a, s[i].b,s[i].skew,
                          s[i].min,s[i].max,s[i].Primery,s[i].Secondary,s[i].SP);


    function1.cpp
    void writesecondline (s[i].id, s[i].chanell_id, s[i].ph, s[i].ccmb,
     s[i].UU, s[i].a, s[i].b,s[i].skew,
           s[i].min,s[i].max,s[i].Primery,s[i].Secondary,s[i].SP){
           ofstream file("fileConfiguration.cfg");
           for(size_t i=0;i<s.size();++i)
           file<<s[i].id<<","<<s[i].chanell_id<<","<<s[i].ph<<","<<s[i].ccmb<<","<<s[i].UU<<","<<s[i].a<<","<<s[i].b<<","<<s[i].skew<<
           ","<<s[i].min<<","<<s[i].max<<","<<s[i].Primery<<","<<s[i].Secondary<<","<<s[i].SP<<"\n";}


    main.cpp
    InfAnalogChanel a;
      InfAnalogChanel b;
    
      a.id=1;
      a.chanell_id="ток ф А";
      a.ph="";
      a.ccmb="";
      a.UU="A";
      a.a=1.290489E-01;
      a.b=0;
      a.skew=0;
      a.min=-32767;
      a.max=32767;
      a.Primery=3000.00000000;
      a.Secondary=1.00000000;
      a.SP="P";
    
      b.id=2;
      b.chanell_id="ток В";
      b.ph="";
      b.ccmb="";
      b.UU="A";
      b.a=5.139708E-02;
      b.b=0;
      b.skew=0;
      b.min=-32767;
      b.max=32767;
      b.Primery=3000.00000000;
      b.Secondary=1.00000000;
      b.SP="P";
    
      writesecondline(s[i].id, s[i].chanell_id, s[i].ph, s[i].ccmb, s[i].UU, s[i].a, s[i].b,s[i].skew,
         s[i].min,s[i].max,s[i].Primery,s[i].Secondary,s[i].SP);
  • Значения не записываются в файл. Почему? с++?

    @kytcenochka Автор вопроса
    Богдан, спасибо! Передала параметрами.
    Подскажите, пожалуйста, а если мне нужно записать файл, в котором n-строк. Каждая строка разной длины. Как лучше сделать?
    Записывать каждую строку отдельной функцией, наверное, глупо.