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

أخذ قيمة معينة من ملف

بدأه ABCDEFG في 5 يوليو 2009 · 3 رد · 380 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

لدينا ملف للدخل فيه عدة أسطر .. في كل سطر مجموعة قيم معينة

مثال :

1 3

5 9 4

5 7 8

كيف يمكننا أخذ القيمة الثالثة في السطر الثاني ووضعها في متغير

و بشكل عام ليكن لدينا n سطر

كيف يمكن أخذ القيمة ال m الموجودة في السطر n-b

حيث m و b أعداد طبيعية ...

#2

ما هو نوع البيانات في الملف و كيفية تنسيقها؟

#3

أتمنى أن لا يكون هذا واجباً أيضاً :)

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

int main()
{
	std::vector< std::vector<int> > data;
	unsigned int b, m, n;
	std::ifstream input("input.txt");
	int element;


	std::string line;
	while(std::getline(input, line))
	{
		std::stringstream convertor(line);
		std::vector<int> numbers;
		int number;

		while(convertor >> number)
		{
			numbers.push_back(number);
		}

		data.push_back(numbers);
	}

	n = data.size();
	std::cout << "enter b and m, seperated by a space: ";
	std::cin >> b >> m;

	try
	{
		element = data.at(n-b).at(m-1);
	}
	catch(std::exception& e)
	{
		std::cout << e.what();

		return 1; // the element does not exist.
	}

	std::cout << "element at line: " << b-n << " and offset m: " << m << " is " << element;

	return 0;
}
#4

السلام عليكــم ورحمـة الله وبركاتــه ،،

بارك الله فيك أخى خالد

استخدام الفيكتور و الإسستريم

بسط الموضوع جدا

:D

محاولتى بإستخدام المصفوفات العاديه


#include <fstream>
#include<string.h>
using namespace std;
int main()
{
int i=0,j=0,m,b,p=0;
char c[10];
int temp[100];
char line[256];
int numOfLines=0;
ifstream readFile("value.txt",ios::in);
while (!readFile.eof())// to calculate number of lines
{
readFile.getline(line,256);
numOfLines++;
}
cout<<"Enter value of b,m "; cin>>b>>m;
readFile.clear();
readFile.seekg(0);
while (i<=numOfLines-b)
{
readFile.getline(line,256);
i++;
}
for (i=0;i<strlen(line);i++) // to make array of int element
{
if(line!=' ')
c[p++]=line;
if((line==' ' && p>0) || i== strlen(line)-1)
{
temp[j]=atoi(c);
p=0;
j++;
}
}
if(m<=j && b<=numOfLines)
{
cout<<" number of lines : "<<numOfLines<<endl;
cout<<" n-b = "<<numOfLines-b<<endl;
cout<<" element in line "<<numOfLines-b<<" and in offset "<<m<<" is "<< temp[m]<<endl;
}

else
cout<<" error !!\n";
return 0;
}
#include <iostream>

ملحوظه::

طبعا موقع العنصر فى السطر يبدأ من الصفر

وكذلك ارقام السطور

بالتوفيق

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

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