إدخال البيانات النصية في ++C

أرسل من قبل Lattakia Girl في الجمعة, 2008/08/15 - 6:42pm.
صورة Lattakia Girl

تاريخ التسجيل: 2008-05-17
مشاركات: 151

الجامعة: تشرين
الكلية: الهندسة المعلوماتية
المرحلة: السنة الثانية
الاختصاص: غير ذلك

مرحبا..

التعليمة cin.getline تتيح إدخال سطر من النص و تخزينه في سلسلة محرفية..
إذا بدنا ندخل أكتر من سطر ( مثلاً 3 سطور ) تحتوي على فراغات من ملف نصي مثلاً, شو بيكون الحل ؟

- سينتهي الأمر بي شرحات سجق مفرومة.
- الحياة !

 
دخول أو تسجيل لإرسال التعليقات | قراءة: 152

خيارات عرض التعليقات

اختر طريقتك المفضلة لعرض التعليقات و اضغط "حفظ الإعدادات" لتفعيل تغييراتك.
الجمعة, 2008/08/15 - 11:16pm
عضو فعال
صورة strontium90

تاريخ التسجيل: 2004-04-21
مشاركات: 3128

الجامعة: حلب
الكلية: الهندسة المعلوماتية
المرحلة: ماجستير
الاختصاص: ذكاء صنعي

You have two options, either you use the getline(std::istream, std::string, char) function defined in <string> or that you use the std::istream member function getline(char*, int, char). Third argument in both functions indicates the line delimiter which defaults to '\n'.
Examples follow using both approaches.
/*
* ftest1.cpp -- Demonstrate getline(std::istream, std::string, char)
* Open a textfile, read first three lines, send them to
* std::cout
*/

#include <string>
#include <fstream>
#include <iostream>
using namespace std ;

int main()
{
ifstream is("somefile.txt" ) ;
string line ;
for(int i = 1 ; i <= 3 ; ++i) {
getline(is, line) ;
cout << line << endl ;
}
}
/*
* ftest2.cpp -- Demonstrate istream::getline(char*, int, char)
* Open a textfile, read first three lines, send them to
* std::cout
*/

#include <string>
#include <fstream>
#include <iostream>
using namespace std ;

int main()
{
const int bufferSize = 100 ;
ifstream is("somefile.txt" ) ;
char line[bufferSize] ;
for(int i = 1 ; i <= 3 ; ++i) {
is.getline(line, bufferSize) ;
cout << line << endl ;
}
}
I'd recommend that you use the former approach, it doesn't impose fixed buffer size limitations and yields in cleaner, more maintainable code.

Read the rules
Use the search engine

Believe in healthy, hearty laughter, at the expense of the whole human race, if needs be.
H. Allen Smith

 
دخول أو تسجيل لإرسال التعليقات
الجمعة, 2008/08/15 - 11:30pm
صورة Lattakia Girl

تاريخ التسجيل: 2008-05-17
مشاركات: 151

الجامعة: تشرين
الكلية: الهندسة المعلوماتية
المرحلة: السنة الثانية
الاختصاص: غير ذلك

شكراً strontium90
بس أنا بقصد انو تتخزن الأسطر التلاتة بالمصفوفة.

- سينتهي الأمر بي شرحات سجق مفرومة.
- الحياة !

 
دخول أو تسجيل لإرسال التعليقات
السبت, 2008/08/16 - 12:16am
عضو فعال
صورة strontium90

تاريخ التسجيل: 2004-04-21
مشاركات: 3128

الجامعة: حلب
الكلية: الهندسة المعلوماتية
المرحلة: ماجستير
الاختصاص: ذكاء صنعي

كتب Lattakia Girl:
أنا بقصد انو تتخزن الأسطر التلاتة بالمصفوفة.

Read and append using the std::string operator +=

// ...

string line ;
string threeLines ;
for(int i = 1 ; i <= 3 ; ++i) {
getline(is, line) ;
threeLines += line ;
}

After that if you want the C-style character string i.e. char* or char[], use the std::string member function c_str.

Read the rules
Use the search engine

Believe in healthy, hearty laughter, at the expense of the whole human race, if needs be.
H. Allen Smith

 
دخول أو تسجيل لإرسال التعليقات
السبت, 2008/08/16 - 12:49am
صورة Lattakia Girl

تاريخ التسجيل: 2008-05-17
مشاركات: 151

الجامعة: تشرين
الكلية: الهندسة المعلوماتية
المرحلة: السنة الثانية
الاختصاص: غير ذلك

شكراً مرة تانية .
بس شو عن طول النص ؟
ما عم يرضى يتعامل مع strlen

- سينتهي الأمر بي شرحات سجق مفرومة.
- الحياة !

 
دخول أو تسجيل لإرسال التعليقات
السبت, 2008/08/16 - 2:37am
عضو فعال
صورة strontium90

تاريخ التسجيل: 2004-04-21
مشاركات: 3128

الجامعة: حلب
الكلية: الهندسة المعلوماتية
المرحلة: ماجستير
الاختصاص: ذكاء صنعي

كتب Lattakia Girl:
شكراً مرة تانية . بس شو عن طول النص ؟ ما عم يرضى يتعامل مع strlen

Of course it won't, strlen defined in <cstring> accepts a single argument of type const char*. With std::string, you use member function length e.g. line.length(). Review your C++ standard library documentation.

 

Read the rules
Use the search engine

Believe in healthy, hearty laughter, at the expense of the whole human race, if needs be.
H. Allen Smith

 
دخول أو تسجيل لإرسال التعليقات