64,691
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
unsigned char a[2][4];
int x,y,z;
FILE *f;
void main() {
f = fopen("tmp.txt", "w+");
// f = fopen("test_save.txt", "r");
// fprintf(f, "a = {{0,1,2,3},{0,2,5,8},}");
// fprintf(f, "aa 0,1,2,3,4,5,6,7,-,a,d");
fprintf(f,
"abc 77 def8"
"g0d"
"*4&^ 2d3m1"
"a5ad3so");
fflush(f);
rewind(f);
printf("---- before loop ----\n");
getchar();
z = 0;
#if 1
for (y = 0; y < 2; y++)
{
for (x = 0; x < 4; x++)
{
{
while (1)
{
int nRet = fscanf(f,"%d",&a[y][x]);
printf("%d, a[%d][%d], %d\n", ++z, y, x, nRet);
if (1==nRet)
{
printf("---%d\n", a[y][x]);
break;
}
else
{
char szch = fgetc(f);
printf("---%c\n", szch);
}
getchar();
}
}
}
}
printf("-----2222----\n");
fclose(f);
remove("tmp.txt");
#endif
}
#include <stdio.h>
#include <stdlib.h>
#define C_ARRAY_SIZE_Y (5)
#define C_ARRAY_SIZE_X (4)
unsigned char szChar;
//unsigned char a[C_ARRAY_SIZE_Y][C_ARRAY_SIZE_X];
//unsigned char a[C_ARRAY_SIZE_Y * C_ARRAY_SIZE_X];
int x,y,z;
FILE *f;
void main() {
#if 1
f = fopen("tmp.txt", "w+");
// fprintf(f, "a = {{0,1,2,3},{0,2,5,8},}");
// fprintf(f, "aa 0,1,2,3,4,5,6,7,-,a,d");
fprintf(f,
"a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20u21v22w23x24y25z26");
fflush(f);
#else
f = fopen("tmp.txt", "r");
#endif
rewind(f);
printf("---- before loop ----\n");
getchar();
z = 0;
#if 1
for (y = 0; y < C_ARRAY_SIZE_Y; y++)
{
for (x = 0; x < C_ARRAY_SIZE_X; x++)
{
{
while (1)
{
int nRet = fscanf(f, "%d", &szChar);
// int nRet = fscanf(f,"%d",&a[y][x]);
// int nRet = fscanf(f, "%d", &a[y * C_ARRAY_SIZE_X + x]);
printf("%d, a[%d][%d], %d\n", ++z, y, x, nRet);
if (1==nRet)
{
printf("---[%d]\n", szChar);
// printf("---[%d]\n", a[y][x]);
// printf("---[%d]\n", a[y * C_ARRAY_SIZE_X + x]);
break;
}
else if (EOF == nRet)
{
break;
}
else
{
char szch = fgetc(f);
printf("---[%c]\n", szch);
}
// getchar();
}
}
}
}
#endif
printf("-----2222----\n");
fclose(f);
// remove("tmp.txt");
printf("----------9999----------\n");
#if 0
for (y = 0; y < C_ARRAY_SIZE_Y; y++)
{
for (x = 0; x < C_ARRAY_SIZE_X; x++)
{
printf("%d ", a[y][x]);
printf("%d ", a[y * C_ARRAY_SIZE_X + x]);
}
printf("\n");
}
#endif
}
int nRet = fscanf(f,"%d",&a[y][x]);
printf("%d, a[%d][%d], %d\n", ++z, y, x, nRet); //改为%c
if (1==nRet)
{
printf("---%d\n", a[y][x]); //改为%c
break;