1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "stdlib.h"
#define MAX 100
#define BUF_SIZE 256
TCHAR MapObjName[]=TEXT("MyFileMappingObject");
void CreateFile(char *FileName, int a[][MAX], int &n);
void ReadFile(char *FileName, int a[][MAX], int n);
void PrintMatrix(int a[][MAX], int n);
int main()
{
//Usual Task for 2D Array
int a[MAX][MAX];
int n=0;
CreateFile("input.txt",a,n);
ReadFile("input.txt",a,n);
PrintMatrix(a,n);
//END
//Windows Programming with API
HANDLE hMapFile;
LPCTSTR pBuf;
//Create File Mapping
hMapFile = CreateFileMapping
(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
MapObjName
);
//End Create File Mapping
//Check if creation success
if(hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
{
printf("\nCannot create file mapping: %d",GetLastError());
exit(1);
}
else
printf("\nCreate File Mapping success!");
//Map View of File
pBuf = (LPCTSTR)MapViewOfFile
(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE
);
//Check if Create success
if(pBuf==NULL)
{
printf("\nCannot Create Map View of File: %d",GetLastError());
exit(1);
}
//Send Information to shared memory
CopyMemory((PVOID)pBuf,a,512*(n+1));
printf("\n\nSending Matrix Information...\n");
getch();
CopyMemory((PVOID)pBuf,n,BUF_SIZE);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
//END
getch();
return 0;
}
void ReadFile(char *FileName, int a[][MAX], int n)
{
int temp;
FILE *File;
File=fopen(FileName,"r");
if(File==NULL)
{
printf("\nCannot open %s",FileName);
printf("\nPause Program and Exiting...");
system("PAUSE");
exit(-1);
}
fscanf(File,"%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
fscanf(File,"%d",&temp);
a[i][j]=temp;
}
fclose(File);
}
void CreateFile(char *FileName, int a[][MAX], int &n)
{
FILE *f;
int ran; //Create Random Matrix
printf("\n Enter the size of Matrix: ");
scanf("%d",&n);
printf("\nDo you want program automatically create a matrix? 1/0: ");
scanf("%d",&ran);
if(ran==1)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=rand();
f=fopen(FileName,"w");
fprintf(f,"%d",n); //Print size of Matrix
fprintf(f,"\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
fprintf(f,"%10d",a[i][j]);
fprintf(f,"\n");
}
fclose(f);
}
else
{
printf("\nEnter your Matrix\n");
f=fopen(FileName,"w");
fprintf(f,"%d",n); //Print the size of Matrix
fprintf(f,"\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("Column [%d] - Row [%d]: ",i,j);
scanf("%d",&a[i][j]);
fprintf(f,"%10d",a[i][j]);
}
fprintf(f,"\n");
}
fclose(f);
}
}
void PrintMatrix(int a[][MAX], int n)
{
printf("\nMatrix\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
printf("%10d",a[i][j]);
printf("\n");
}
}
|