int值保存初始化

RichAndMerry 2018-06-24 12:56:27
int count = 1;//这里定义的就是一个普通的变量,而且在main函数的开头就初始化了,那就是每次运行这个程序时,count都会先被初始化为1。可是为什么第二次运行时,显示出来的count是接着上一次的数来算的?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 41
int _tmain(int argc, char * argv[])
{
FILE *fp;
char words[MAX];
int count = 1;
char *count_c;

count_c = (char*)malloc(sizeof(char) * 4);
if ((fp = fopen("wordy", "a++")) == NULL)
{
fprintf(stdout, "Can't open \"wordy\" file.\n");
exit(EXIT_FAILURE);
}
puts("Enter words to add to the file; press the #");
puts("key at the begining of a line to terminate.");
while ((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#'))
fprintf(fp, "%s\n", words);
puts("File contents:");
rewind(fp);
while (fscanf(fp,"%s", words) == 1)
{
_itoa(count,count_c,10);
printf("%s %s\n", count_c, words);
++count;
}
puts("Done!");
if (fclose(fp) != 0)
{
fprintf(stderr, "Error closing file\n");
}
free(count_c);
system("PAUSE");
return 0;
}
...全文
177 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
RichAndMerry 2018-06-29
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define Length 10
#define Amount 5
int _tmain(int argc, char * argv[])
{
struct three_name{
char last_name[Length];
char mid_name[Length];
char first_name[Length];
};
struct member{
char account[Length];
struct three_name name;
};
struct member m_member[Amount] = { { "101", { "aaa", "bb2", "dccc" } }, { "202", { "abb", .mid_name = "fccc" } }, { "303", { "acc", "bb4", "hccc" } }, { "404", { "add", "bb5", "fccc" } }, { "505", { "aee", "bb6", "ccc" } } };

system("PAUSE");
return 0;
}
AlbertS 2018-06-25
  • 打赏
  • 举报
回复
不会吧,程序第二次运行。count应该还是从1开始的,你可以输出count看一下变量值
RichAndMerry 2018-06-25
  • 打赏
  • 举报
回复
fseek函数一般用于二进制文件,也可以用于文本文件。用于文本文件操作时,需特别注意回车换行的情况:因为在一般浏览工具如UltraEdit中,回车换行视为两个字符0x0D和0x0A,但真实的文件读写和定位时却按照一个字符0x0A进行处理,因此碰到此类问题时,可以考虑将文件整个读入内存,然后在内存中手工插入0x0D的方法,这样可以达到较好的处理效果。
RichAndMerry 2018-06-24
  • 打赏
  • 举报
回复

第二次运行结果
RichAndMerry 2018-06-24
  • 打赏
  • 举报
回复

第一次运行结果
RichAndMerry 2018-06-24
  • 打赏
  • 举报
回复
0 1 2 3 4 5 6 7 8 9
1 8 7 6 5 4 3 2 1 0
2 0 0 0 2 2 2 0 0 0
3 1 2 4 5 6 7 8 6 5
4 5 4 5 4 5 4 5 4 5
5 7 7 7 7 9 9 9 9 9
6 3 3 3 4 4 4 1 1 1
RichAndMerry 2018-06-24
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define Length 20
int _tmain(int argc, char * argv[])
{
char name[Length];
char ch;
int data_n[7][10] = {};
char data_i[8][10] = {};
FILE * fp;
char image_n[10] = { ' ', '.', '/', '-', '+', '*', '&', '^', '#', '@' };
int num = 0, value = 0;
printf("Please enter the file's name:");
scanf("%s",name);
if ((fp = fopen(name, "r")) == NULL)
{
printf("Cna't open %s file.", name);
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < 7; i++)
{
for (size_t j = 0; j < 10; j++)
{
//fseek(fp, fp_n, SEEK_SET);
ch = fgetc(fp);
//ch = getc(fp);
value = (int)(ch - '0');
data_n[i][j] = value;
fgetc(fp);
//fp_n = fp_n + 2;
}
//fp_n = fp_n - 1;
}
for (size_t i = 0; i < 7; i++)
{
for (size_t j = 0; j < 10; j++)
{
num = data_n[i][j];
data_i[i][j] = image_n[num];
}
}
for (size_t i = 0; i < 10; i++)
{
data_i[7][i] = image_n[1];
}
for (si
wang0635 2018-06-24
  • 打赏
  • 举报
回复
打印出来的count,是读出文件中的单词的计数。第二次运行时,文件中追加了一句话后,却又从头开始读出,当然要有前面的东西了
赵4老师 2018-06-24
  • 打赏
  • 举报
回复
《程序员的自我修养——链接、装载与库》

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧