# include <stdlib.h>
# include <stdio.h>
# include <time.h>
# include <string.h>
int main ( );
void test01 ( );
void test02 ( );
void filename_inc ( char *filename );
int prime ( int n );
double *r8vec_uniform_01_new ( int n, int *seed );
int s_len_trim ( char *s );
void timestamp ( );
/******************************************************************************/
int main ( )
/******************************************************************************/
/*
Purpose:
FILES_MULTIPLE demonstrates how to work with multiple files.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
03 December 2012
Author:
John Burkardt
*/
{
timestamp ( );
printf ( "\n" );
printf ( "FILES_MULTIPLE:\n" );
printf ( " C version.\n" );
printf ( " Demonstrate how to work with multiple files.\n" );
test01 ( );
test02 ( );
/*
Terminate.
*/
printf ( "\n" );
printf ( "FILES_MULTIPLE:\n" );
printf ( " Normal end of execution.\n" );
printf ( "\n" );
timestamp ( );
return 0;
}
/******************************************************************************/
void test01 ( )
/******************************************************************************/
/*
Purpose:
TEST01 writes data to four files which are open simultaneously.
Discussion:
The files contain the integers which are divisible by 2, 3, 5, and 7.
divisor1.txt divisor2.txt divisor3.txt divisor4.txt
0 0 0 0
2 3 5 7
4 6 10 14
6 9 15 21
8 12 20 28
...
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
03 December 2012
Author:
John Burkardt
*/
{
# define FILENUM 4
/*
FILENUM can be increased, and the program will still work the same way.
*/
int divisor[4];
char filename[4][80];
FILE *fileunit[4];
int i;
int j;
char template[] = "divisor0.txt";
printf ( "\n" );
printf ( "TEST01\n" );
printf ( " Create 4 files, and count to 100.\n" );
printf ( " \"divisor1.txt\" will contain multiples of 2;\n" );
printf ( " \"divisor2.txt\" will contain multiples of 3;\n" );
printf ( " \"divisor3.txt\" will contain multiples of 5;\n" );
printf ( " \"divisor4.txt\" will contain multiples of 7;\n" );
/*
Set the divisors.
PRIME is a function which can return the I-th prime number.
*/
for ( i = 0; i < FILENUM; i++ )
{
divisor[i] = prime ( i + 1 );
}
/*
Define the file names for an arbitrary number of files
by incrementing the numeric information in a "template".
*/
for ( i = 0; i < FILENUM; i++ )
{
filename_inc ( template );
strcpy ( filename[i], template );
}
/*
Associate a unit number with each file.
*/
for ( i = 0; i < FILENUM; i++ )
{
fileunit[i] = fopen ( filename[i], "wt" );
fprintf ( fileunit[i], "Multiples of %d\n", divisor[i] );
}
for ( j = 0; j <= 100; j++ )
{
for ( i = 0; i < FILENUM; i++ )
{
if ( ( j % divisor[i] ) == 0 )
{
fprintf ( fileunit[i], "%d\n", j );
}
}
}
/*
Close the files.
*/
for ( i = 0; i < FILENUM; i++ )
{
fclose ( fileunit[i] );
}
return;
# undef FILENUM
}
/******************************************************************************/
void test02 ( )
/******************************************************************************/
/*
Purpose:
TEST02 writes selected data to four files which are open simultaneously.
Discussion:
The vector X contains 100 random numbers, and we update X 20 times.
We want to keep track of X(10), X(25), X(64) and X(81) on each step.
x1.txt x2.txt x3.txt x4.txt
X(10) values X(25) values X(64) values X(81) values
1 2.1 1.7 5.3 9.8
2 2.0 3.6 6.2 7.3
3 2.5 4.1 5.9 6.8
..
10 5.0 5.2 5.3 5.6
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
03 December 2012
Author:
John Burkardt
*/
{
# define FILENUM 4
/*
FILENUM can be increased, but more entries in K would have to be defined
by the user.
*/
char filename[4][80];
FILE *fileunit[4];
int i;
int i2;
int j;
int k[4] = { 9, 24, 63, 80 };
int n = 100;
int seed;
char template[] = "x0.txt";
double *x;
double y[100];
printf ( "\n" );
printf ( "TEST02\n" );
printf ( " Create 4 files.\n" );
printf ( " The vector X contains 100 values,\n" );
printf ( " and is set 20 times.\n" );
printf ( "\n" );
printf ( " Write X[9], X[24], X[63] and X[80] to the files\n" );
printf ( " \"x1.txt\", \"x2.txt\", \"x3.txt\" and \"x4.txt\"\n" );
/*
Define the file names for an arbitrary number of files
by incrementing the numeric information in a "template".
*/
for ( i = 0; i < FILENUM; i++ )
{
filename_inc ( template );
strcpy ( filename[i], template );
}
/*
Associate a unit number with each file.
*/
for ( i = 0; i < FILENUM; i++ )
{
fileunit[i] = fopen ( filename[i], "wt" );
fprintf ( fileunit[i], "Values of X[%d]:\n", k[i] );
}
/*
Initial X vector is random.
*/
seed = 123456789;
x = r8vec_uniform_01_new ( n, &seed );
/*
Write selected data to file, then update data.
*/
for ( j = 0; j <= 20; j++ )
{
for ( i = 0; i < FILENUM; i++ )
{
fprintf ( fileunit[i], " %2d %14.6g\n", j, x[k[i]] );
}
if ( j < 20 )
{
y[0] = ( x[0] + x[1] ) / 2.0;
for ( i2 = 1; i2 < 99; i2++ )
{
y[i2] = ( x[i2-1] + x[i2] + x[i2+1] ) / 3.0;
}
y[99] = ( x[98] + x[99] ) / 2.0;
for ( i2 = 0; i2 < 100; i2++ )
{
x[i2] = y[i2];
}
}
}
/*
Close the files.
*/
for ( i = 0; i < FILENUM; i++ )
{
fclose ( fileunit[i] );
}
return;
# undef FILENUM
}
/******************************************************************************/
void filename_inc ( char *filename )
/******************************************************************************/
/*
Purpose:
FILENAME_INC increments a partially numeric file name.
Discussion:
It is assumed that the digits in the name, whether scattered or
connected, represent a number that is to be increased by 1 on
each call. If this number is all 9's on input, the output number
is all 0's. Non-numeric letters of the name are unaffected.
If the name is empty, then the routine stops.
If the name contains no digits, the empty string is returned.
Example:
Input Output
----- ------
"a7to11.txt" "a7to12.txt" (typical case. Last digit incremented)
"a7to99.txt" "a8to00.txt" (last digit incremented, with carry.)
"a9to99.txt" "a0to0.txt" (wrap around)
"cat.txt" " " (no digits to increment)
" " STOP! (error)
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
22 November 2011
Author:
John Burkardt
Parameters:
Input/output, char *FILENAME, the filename to be incremented.
*/
{
int change;
int n;
char *t;
n = s_len_trim ( filename );
if ( n <= 0 )
{
fprintf ( stderr, "\n" );
fprintf ( stderr, "FILENAME_INC - Fatal error!\n" );
fprintf ( stderr, " The input string is empty.\n" );
exit ( 1 );
}
change = 0;
t = filename + n - 1;
while ( 0 < n )
{
if ( '0' <= *t && *t <= '9' )
{
change = change + 1;
if ( *t == '9' )
{
*t = '0';
}
else
{
*t = *t + 1;
return;
}
}
t--;
n--;
}
/*
No digits were found. Return blank.
*/
if ( change == 0 )
{
n = s_l
没有合适的资源?快使用搜索试试~ 我知道了~
C 代码 一次打开多个输出文件, 并将数据写入它选择的任何一个特定文件.rar
共2个文件
c:1个
sh:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2022-11-13
21:16:15
上传
评论
收藏 8KB RAR 举报
温馨提示
C++源码,C源码
资源推荐
资源详情
资源评论
收起资源包目录
C 代码 一次打开多个输出文件, 并将数据写入它选择的任何一个特定文件.rar (2个子文件)
files_multiple
files_multiple.c 23KB
files_multiple.sh 273B
共 2 条
- 1
资源评论
卷积神经网络
- 粉丝: 339
- 资源: 8460
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功