Write a C program to read a binary file, and write a binary file. Write out the characters read, replacing any unprintable characters with a question mark.
Remember the assignment where you printed the arguments passed to your program from the command line? We will take this further in this homework. Your program should process the parameter list. The first argument should be the input file, and the second one should be the output file. If the user passes \"-h\" to your program, write out a \"help\" message talking about the program, and quit. If the user does not specify exactly 2 filenames, also write out the help message and quit. Make sure to check that the input file exists.
For the purpose of this assignment, we consider characters 32 to 127 to be printable. Also, the line-feed character is printable. All other characters should be considered unprintable . Page 545 of your book contains an example of opening and closing a file. You can use \"getc\" to get a character from a file, and \"putc\" to put a character to a file.
Write a C program to read a binary file, and write a binary file. Write out the characters read, replacing any unprintable characters with a question mark.
Remember the assignment where you printed the arguments passed to your program from the command line? We will take this further in this homework. Your program should process the parameter list. The first argument should be the input file, and the second one should be the output file. If the user passes \"-h\" to your program, write out a \"help\" message talking about the program, and quit. If the user does not specify exactly 2 filenames, also write out the help message and quit. Make sure to check that the input file exists.
For the purpose of this assignment, we consider characters 32 to 127 to be printable. Also, the line-feed character is printable. All other characters should be considered unprintable . Page 545 of your book contains an example of opening and closing a file. You can use \"getc\" to get a character from a file, and \"putc\" to put a character to a file.
Write a C program to read a binary file, and write a binary file. Write out the characters read, replacing any unprintable characters with a question mark.
Remember the assignment where you printed the arguments passed to your program from the command line? We will take this further in this homework. Your program should process the parameter list. The first argument should be the input file, and the second one should be the output file. If the user passes \"-h\" to your program, write out a \"help\" message talking about the program, and quit. If the user does not specify exactly 2 filenames, also write out the help message and quit. Make sure to check that the input file exists.
For the purpose of this assignment, we consider characters 32 to 127 to be printable. Also, the line-feed character is printable. All other characters should be considered unprintable . Page 545 of your book contains an example of opening and closing a file. You can use \"getc\" to get a character from a file, and \"putc\" to put a character to a file.
using System;
using System.IO;
namespace BinaryFileApplication
{
class Program
{
static void Main(string[] args)
{
BinaryWriter bw;
BinaryReader br;
int i = 25;
double d = 3.14157;
bool b = true;
string s = \"I am happy\";
//create the file
try
{
bw = new BinaryWriter(new FileStream(\"mydata\", FileMode.Create));
}
catch (IOException e)
{
Console.WriteLine(e.Message + \"\ Cannot create file.\");
return;
}
//writing into the file
try
{
bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
}
catch (IOException e)
{
Console.WriteLine(e.Message + \"\ Cannot write to file.\");
return;
}
bw.Close();
//reading from the file
try
{
br = new BinaryReader(new FileStream(\"mydata\", FileMode.Open));
}
catch (IOException e)
{
Console.WriteLine(e.Message + \"\ Cannot open file.\");
return;
}
try
{
i = br.ReadInt32();
Console.WriteLine(\"Integer data: {0}\", i);
d = br.ReadDouble();
Console.WriteLine(\"Double data: {0}\", d);
b = br.ReadBoolean();
Console.WriteLine(\"Boolean data: {0}\", b);
s = br.ReadString();
Console.WriteLine(\"String data: {0}\", s);
}
catch (IOException e)
{
Console.WriteLine(e.Message + \"\ Cannot read from file.\");
return;
}
br.Close();
Console.ReadKey();
}
}
}