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

الرجاء المساعدة

مغلق
بدأه بندر بن ياسر في 2 مايو 2007 · 7 رد · 597 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

I'm write a code in c++. that code read an input data from a file,this is apart of my code:



// .cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdio.h>
#include "stdafx.h"
//-------------------------------------------
using namespace std;
int main(int argc, char* argv[])
{
char inFileName[100];
printf("Hello World!\n");
if(argc<2)
{
cout<<"Please enter an input file name\n";
exit(1);
}
strcpy(inFileName,argv[1]);
ifstream f(inFileName,ios::in |ios::binary);
if(!f)
{
cout<<"Can't open file.\n";
return 1;
}
return 1;
}

when i do the run the file is not exit.

it print please enter file name.

please help me,and another qustion, the statement ifstream f(inFileName,ios::in|ios::binary) help me to open the file or i should to write the open file statement.

Thanks

#2

السلام عليكم ,,

أخي العزيز ,, معذرة و لكن أرجو كتابة السؤال باللغة العربية حتى نستطيع فهم المطلوب ,,

لأن قواعد الانجليزي عندك مضروبة ( :D ) :)

إن شاء الله بالعربي و نعطيك التعديل المطلوب ,,

تحياتي ,,

#3

مرحبا اخوي خالد .

سؤالي هو :في البرنامج لما طلبت يفتح ملف اسمه موجود في [argv[1

مايفتحه يعني يدخل في الشرط ( if(!f

مالسبب!!

#4

السلام عليكم

أخي لم تعرف إسم الملف الذي تريد فتحه لذلك عند طلب كتابة الإسم أضف أيضا إمكانية قراءة الإسم من لوحت المفاتيح

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdio.h>
#include<conio.h>

using namespace std;

int main(int argc, char* argv[])
{
char inFileName[100];
printf("Hello World!\n");
 if(argc<2)
  { cout<<"Please enter an input file name\n";
	cin>>inFileName;  
  }
strcpy(inFileName, "C:\\aa.txt");
ifstream f(inFileName,ios::in |ios::binary);
 if(!f)
  { cout<<"Can't open file.\n";
	return 1;
  }
  else{cout<<"file opened.\n";}
  getch();
  return 1;
}

أما التعليمات التالية فهي لتعريف كيفية فتح الملف: كتابة, قراءة, تحديث...

,ios::in|ios::binary

هذا مثال لكيفية قراءة محتوى النص و إظهاره على الشاشة

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdio.h>
#include<conio.h>
# include <sstream>
# include <string>

using namespace std;

int main(int argc, char* argv[])
{
 ifstream f("c:\\aa.txt");

 if(!f)
  { cout<<"Can't open file.\n";  }
	std::string line;
	std::istringstream iss;
	std::getline(f, line);
	iss.str(line);

	cout<<line;
	std::getline(f, line);

getch();
return EXIT_SUCCESS;
}

ولا تنسى أن تضع دائما هذه المكتبة في أول أسماء المكتبات التي تستدعيها

#include "stdafx.h"

والله أعلم

#5

الأخ الفاضل ...

جرب استخدام

#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <string.h>

int main(int argc, char* argv[])
{
	char	inFileName[260];

	cout << "Hello World!" << endl;

	if(argc<2)
	{
		cout << "Please enter an input file name" << endl;
		return 2;
	}

	strcpy(inFileName, argv[1]);

	ifstream f(inFileName, ios::in | ios::binary);
	if(!f)
	{
		cout << "Can't open file." << endl;
		return 1;
	}
	//
	// Do your file tasks here
	//
	f.close();
	return 0;
}

وعند استدعاء البرنامج من سطر الأوامر (بأفتراض أسم البرنامج ofile.exe) لفتح ملف اسمه (C:\readme.txt) استخدم

ofile.exe C:\readme.txt

شكراً

Eng. Usama El-Mokadem

Nothing is impossible, the word impossible itself says that: I M - Possible

#6

شكرا على ردك..

حاولت كتابة اسم الملف ولكن نفس المشكلة..

وبالنسبة لاسم المكتبة هل ضروري وضعها .

#7

شكراً اخوي اسامة.

#8

السلام عليكم

قم بإنشاء ملف c:\aa.txt وسيعمل البرنامج.

لأني عرفت إسم ومسار الملف في الكود. وعندما جربته في حاسوبك لم يعمل لأنك لم تنشأ ذلك الملف

أما المكتبةstdafx فهو إجباري إستعمالها في الفيزوال ستوديو دوت نت وليس الحال ذاته في بقية برامج الترجمة Turbo C , C Builder ...

والله أعلم

هذا الموضوع مغلق.

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