开发平台:visual studio 2008
随机数:
产生随机数,每次产生的数字都不相同。
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>//for scrand() & system()
#include<time.h>//for time()
int main(int argc, char* argv[])
{
srand((int)time(0));
for(int x=0;x<10;x++)
printf("%d\n",rand()%100);
system("PAUSE");//create a pause and waite for pressing any key
return 0;
}
文件读写:
#include "stdafx.h"
#include <stdio.h>
#include<stdlib.h>//for file-operating
int main(int argc, char* argv[])
{
FILE* fp=fopen("E:\\MPI\\textfileReadWrite\\Debug\\b.txt", "r");
char line[40] = {0};
if(fp)
{
//read a file
while(!feof(fp))
{
fscanf(fp, "%s", line);
printf("%s\n", line);
}
fclose(fp);//can't be forgoted.
//write into a file
//when open a file with 'w' mode,the content of the file was cleared.
FILE* fp2=fopen("E:\\MPI\\textfileReadWrite\\Debug\\b.txt", "w+");
fprintf(fp2,"%s\n","lab513@zhu");
fclose(fp2);
//read the file again
fp=fopen("E:\\MPI\\textfileReadWrite\\Debug\\b.txt", "r");
while(!feof(fp2))
{
fscanf(fp2, "%s", line);
printf("%s\n", line);
}
fclose(fp);
}
else
fclose(fp);
system("PAUSE");
return 0;
}
------------------
snigoal@Wang,HUST.Wuhan.China

没有评论:
发表评论