السلام عليكم ورحمة الله وبركاته
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main ( int argc, char * argv[] )
{
//here argc should be 4 since we have 4 arguments
//argv is an array of strings, each string holding an argument
//argv[0] equals the filename "mbad", argv[1] equals "-a" or "-d"
//argv[2] equals the input file name, argv[3] equals the output file name
string input1;
// contain file data input from the user
if (! ( ( argc == 4) && ( strcmp(argv[1],"-a")==0 || strcmp(argv[1],"-d")==0 ) ) )
{
//here if the user enters the command line arguments incorrectly, we print a
//help message as specified in the project pdf file
printf( "Usage %s [-h|-a|-d] [fname1 fname2]\n", argv[0]);
printf ("-a takes assembly and produces machine code, both file names must be given\n");
printf ("-d takes machine code and produces assembly, both file names must be given\n");
}
else
{
FILE * fIn = fopen ( argv[2], "r" );
FILE * fOut = fopen ( argv[3], "w" );
if ( fIn == NULL )
{
printf( "Could not open input file\n" );
}
else
{
if ( strcmp(argv[1],"-a")==0 )
{
//Convert from assembly to machine code
fIn >> input1;
while ( !fIn.eof() )
{
// keep reading until end-of-file
cout << input1;
fIn >> input1; // sets EOF flag if no value found
}
//...
//...
printf ("Conversion from assembly to machine code done successfully\n");
}
else // here argv[1] will be equal "-d"
{
//Convert from machine code to assembly
//...
//...
printf ("Conversion from machine code to assembly done successfully\n");
}
fclose (fIn);
fclose (fOut);
}
}
}
طلب : انا اول مرة اتعامل مع الملفات فى السى لو يوجد دروس مبسطة للشرح هذا الجزء ارجو وضع الرابط
وجزاكم الله خيرا
