الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[ تمت الإجابة ]مشكلة عند قراءة ملف .txt

بدأه hbate_allah في 9 ديسمبر 2012 · 1 رد · 847 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لدي ملف من الشكل

 -0.01 0.03 0.04 -0.5 0.6 0.8 0.3 -0.005 -0.003 -0.008
-0.03 0.05 0.06 -0.005  -0.2 0.3 0.8 1.02 1.03 1.04 
.........................

اريد قراءته و حفظه في struct

struct Vector3 
{
    float x, y, z;
};

struct pose
{
    int time;

    Vector3 joint_angle;
};

بحيث كل سطر يبدا برقم float time ثم كل 3 ارقام فيما بعد نضعها في joint_angle

و السطر كله عبارة عت pose

للتوضيح

اقتباس
-0.01 is pose[0].time , 0.03 0.04 -0.5 is pose[0].joint_angle[0]........

-0.03 is pose[1].time ,0.05 0.06 -0.005 is pose[1].joint_angle[0]...

عملت كود صغير

struct  joint_angle
{
    int count;

    Vector3 orient;
};
//////////////////////////////
struct pose
{
    float time;
    std::vector<joint_angle> angle;
};

////////////////////////////
std::istream & operator>> (std::istream & stream, joint_angle & ja) 
{
    ja.count = 0;
    stream >> ja.orient.x >> ja.orient.y >> ja.orient.z;
	printf ("ja.orient.x===>%f",ja.orient.x);
	printf ("ja.orient.y===>%f",ja.orient.y);
	printf ("ja.orient.z===>%f",ja.orient.z);
    return stream;
}////////////////////////////////////////////
int main()
{

    std::ifstream stream("angle.txt" );
joint_angle ja;
std::vector<joint_angle> angles;
char c;
while ( stream >> ja >>  std::noskipws >> c ) {
	printf ("ja.orient.x===>%f",ja.orient.x);
	printf ("ja.orient.y===>%f",ja.orient.y);
	printf ("ja.orient.z===>%f",ja.orient.z);
   angles.push_back(ja);
   if (c == '\n' ) {
      stream >> std::skipws; // Reset - because of the reading we have to repeat (see 1)
      // new pose incoming
      // possibly break; here
   }

}
	//}
return 0;
}

لكن المشكلة انو يقرا لي الاربعة ارقام الاولى فقط لا يقرا لي الباقي

اظن ان المشكلة هنا

    std::ifstream stream("angle.txt" );

ارجو منكم مساعدتي

#2

أخي برنامجك معقد وليس مقروء، إذا كنت أنت من تحدد محتويات الملف فبدلاً من حفظ الأرقام في ملف نصيّ، إحفظها كبيانات. إنظر لهذا المثال:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#pragma pack(1)
struct Vector {
    float x;
    float y;
    float z;
};
#pragma pack()

class VectorFile {
    string filename;
public:
    VectorFile(string filename);
    int read(Vector *v, unsigned int position = 0);
    int dump(Vector *v, bool append = false);
};

VectorFile::VectorFile(string filename)
{
    this->filename = filename;
}

int VectorFile::read(Vector *v, unsigned int position)
{
    ifstream f;

    f.open(this->filename.c_str(), ios::in | ios::binary);
    f.seekg(sizeof(Vector) * position);
    f.read((char *) v, sizeof(Vector));
    f.close();
    return 0;
}

int VectorFile::dump(Vector *v, bool append)
{
    ofstream f;

    if( append )
         f.open(this->filename.c_str(), ios::out | ios::binary | ios::app);
    else
        f.open(this->filename.c_str(), ios::out | ios::binary);

    f.write((char *) v, sizeof(Vector));
    f.close();
    return 0;
}

int main(int argc, char **argv)
{
    VectorFile vfile("file.vector");

    Vector vout1 = {10.9, 42.5, 123.12};
    Vector vout2 = {2.76, 221.3, 42.31};
    Vector vout3 = {24.5, 1.35, 93.6};
    Vector vin1;
    Vector vin2;
    Vector vin3;

    vfile.dump(&vout1);
    vfile.dump(&vout2, true);
    vfile.dump(&vout3, true);

    vfile.read(&vin1);
    vfile.read(&vin2, 1);
    vfile.read(&vin3, 2);

    cout << "x1 = " << vin1.x << endl;
    cout << "y1 = " << vin1.y << endl;
    cout << "z1 = " << vin1.z << endl;

    cout << "---------------" << endl;

    cout << "x2 = " << vin2.x << endl;
    cout << "y2 = " << vin2.y << endl;
    cout << "z2 = " << vin2.z << endl;

    cout << "---------------" << endl;

    cout << "x3 = " << vin3.x << endl;
    cout << "y3 = " << vin3.y << endl;
    cout << "z3 = " << vin3.z << endl;

    return 0;
}

سينتج ملف file.vector يحتوي شيء مثل:

ff.A..*Bq=.B..0@.L]Cq=)B...A...?33.B

قراءته والكتابة إليه أسهل عليك من أن تستخدم نصوص.

موضوع مرتبط: /index.php?showtopic=268948&st=0&p=1306948entry1306948

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…