ConsoleApplication39.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <locale.h>
#include <stdio.h >
#include <iostream>
#include <cstdlib>
#include <ctime>
#include<iostream>
using namespace std;
void GenerateDataset(char *filename, int num);
int SortDataset(char *filename, int num);
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(0, "");
char *filename;
filename = new char[250];
int num;
printf("Enter filename: ");
scanf_s("%s", filename);
printf("\n Enter num: ");
scanf_s("%d", &num);
GenerateDataset(filename, num);
SortDataset(filename, num);
system("pause");
return 0;
}
void GenerateDataset(char *filename, int num) {
FILE *mf;
fopen_s(&mf, filename, "w");
if (mf == NULL) printf("Error file open\n");
int i;
int k;
srand(time_t(0));
//for (i=1; i<=num; i++) {
// int i;
for (i = 1; i <= num; i++) {
k = -(int)(rand() % 1000 + 1);
fprintf(mf, "%d \n", k);
}
fclose(mf);
}
int SortDataset(char *filename, int num) {
FILE *mf;
int str;
fopen_s(&mf, filename, "r");
if (mf == NULL) printf("Error file open\n");
int *a = new int[num];
int i, k;
for (i = 1; i <= num; i++) {
fscanf_s(mf, "%d", &a[i]);
printf("%d", a[i]);
}
delete[] a;
fclose(mf);
return 0;
}
0xC0000005: 写入位置 0x00964000 时发生访问冲突。
//
#include "stdafx.h"
#include <locale.h>
#include <stdio.h >
#include <iostream>
#include <cstdlib>
#include <ctime>
#include<iostream>
using namespace std;
void GenerateDataset(char *filename, int num);
int SortDataset(char *filename, int num);
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(0, "");
char *filename;
filename = new char[250];
int num;
printf("Enter filename: ");
scanf_s("%s", filename);
printf("\n Enter num: ");
scanf_s("%d", &num);
GenerateDataset(filename, num);
SortDataset(filename, num);
system("pause");
return 0;
}
void GenerateDataset(char *filename, int num) {
FILE *mf;
fopen_s(&mf, filename, "w");
if (mf == NULL) printf("Error file open\n");
int i;
int k;
srand(time_t(0));
//for (i=1; i<=num; i++) {
// int i;
for (i = 1; i <= num; i++) {
k = -(int)(rand() % 1000 + 1);
fprintf(mf, "%d \n", k);
}
fclose(mf);
}
int SortDataset(char *filename, int num) {
FILE *mf;
int str;
fopen_s(&mf, filename, "r");
if (mf == NULL) printf("Error file open\n");
int *a = new int[num];
int i, k;
for (i = 1; i <= num; i++) {
fscanf_s(mf, "%d", &a[i]);
printf("%d", a[i]);
}
delete[] a;
fclose(mf);
return 0;
}
0xC0000005: 写入位置 0x00964000 时发生访问冲突。