C 从文本文件中逐个计数字符
我的程序需要从文本文件中获取输入并在一行上输出。 但是,如果单个行上的字符数超过60,则应该有一个新行 到目前为止,我掌握的代码是:C 从文本文件中逐个计数字符,c,file,fgets,C,File,Fgets,我的程序需要从文本文件中获取输入并在一行上输出。 但是,如果单个行上的字符数超过60,则应该有一个新行 到目前为止,我掌握的代码是: int main(int argc, char *argv[]){ FILE *pFile; char x[10]; char *token; pFile = fopen("test5.txt","r"); int wordLength; int totalCharLength; char newline[10] = "newline"; i
int main(int argc, char *argv[]){
FILE *pFile;
char x[10];
char *token;
pFile = fopen("test5.txt","r");
int wordLength;
int totalCharLength;
char newline[10] = "newline";
if(pFile != NULL){
while (fgets(x, sizeof(x), pFile) != NULL) {
token = strtok(x,"\r\n");
if(token != NULL){
printf("%s",x);
}
else{
printf("%s",x);
}
//counter++;
wordLength = strlen(x);
totalCharLength += wordLength;
printf("%d",totalCharLength);
if(totalCharLength == 60){
printf("%s",newline);
}
}
fclose(pFile);
}
}
文本文件:
a dog chased
a cat up the
tree. The cat
looked at the dog from
the top of the tree.
有5条独立的线。它显示了这个。这不是一行的全部。
输出:
a dog chased a cat up the tree. The cat looked at the dog from the top of the tree.
现在,程序需要能够以该格式获取输入文本,并在一行中打印出来
所以上面的输出应该在第60个字符处有一个新行,在第二个单词“dog”之前
但是,我想添加一个功能,这样就有一个字符计数器,当字符计数=60时,它会打印一个新行。
现在,经过更多的调试,我添加了代码
printf("%d",totalCharLength);
就在if(totalCharLength==60)行之前,我意识到字符是以随机增量而不是逐个计数的。输出:
a dog cha9sed 13a cat up 22the 26tree. The35 cat 40looked at49 the dog 58 from 63 the top o72f the tre81e. 85
所以这表明它并不是一个字符一个字符地计数。但是,当我将charx[10]更改为较低的值时,它不会在一行上打印所有内容。它会留下缺口
示例:将charx[10]更改为charx[2]
这可以正确地逐字符获取,但是输出不在一行中
只有当一行中的字符超过60个时,才会出现新行。不是14(第一行)
我认为你最好自己打印每个字符,并在添加换行符之前检查空格 比如:
((charCount > 60) && (currentChar == ' ')) {
// print newline and reset counter
printf("\n");
charCount = 0;
}
编辑:
在任何操作之前,检查当前字符是否已经是换行符,并跳过它。
检查回车符\r
,因为在windows中换行符是\r\n
#include <stdio.h>
int main(void) {
FILE *f = fopen("test.txt", "r");
if(!f) {
printf("File not found.\n");
return 1;
}
char c;
int cCount = 0;
while((c = fgetc(f)) != EOF) {
if(c == '\r') continue;
if(c == '\n') c = ' ';
printf("%c", c);
if((cCount++ > 60) && (c == ' ')) {
printf("\n");
cCount = 0;
}
}
fclose(f);
return 0;
}
#包括
内部主(空){
文件*f=fopen(“test.txt”、“r”);
如果(!f){
printf(“未找到文件。\n”);
返回1;
}
字符c;
int cCount=0;
而((c=fgetc(f))!=EOF){
如果(c=='\r')继续;
如果(c=''\n')c='';
printf(“%c”,c);
如果((cCount++>60)和&(c==''){
printf(“\n”);
cCount=0;
}
}
fclose(f);
返回0;
}
打印出令牌后,使用一个单独的数组连接令牌。这将在一行中显示整个文件。从这里解析出60个字符,然后边走边打印行会容易得多。就像这样
#include<stdio.h>
//#pragma warning(disable : 4996)
int main()
{
FILE *pfile;
char data;
int count = 1;
pfile = fopen("test.txt", "r");
printf("Opening file...\n");
if (pfile == NULL)
{
printf("Error!\n");
}
while ((data = fgetc(pfile)) != EOF)
{
if (count != 60)
{
if (data != '\n')
{
printf("%c", data);
count++;
}
}
else
{
printf("\n");
count = 1;
}
}
fclose(pfile);
return 0;
}
#包括
//#杂注警告(禁用:4996)
int main()
{
文件*pfile;
字符数据;
整数计数=1;
pfile=fopen(“test.txt”、“r”);
printf(“打开文件…\n”);
if(pfile==NULL)
{
printf(“错误!\n”);
}
而((数据=fgetc(pfile))!=EOF)
{
如果(计数!=60)
{
如果(数据!='\n')
{
printf(“%c”,数据);
计数++;
}
}
其他的
{
printf(“\n”);
计数=1;
}
}
fclose(pfile);
返回0;
}
在第一种情况下,当您有char x[10]
时,fgets
将最多读取9个字符,或者直到找到换行符。所以输出是
a dog cha9sed 13a cat up 22the 26
因为对fgets
的第一次调用读取9个字符(然后打印9个),第二次调用读取换行符(打印13个),第三次调用读取9个字符(打印22个),第四次调用读取换行符(打印26个)
当您更改为char x[2]
时,fgets
一次只能读取一个字符。这导致strtok的工作方式与您预期的不同。当字符串仅包含分隔符字符时,strtok
将返回NULL
,并且该字符串未被修改。因此,如果字符串只包含换行符,strtok
不会像您预期的那样删除换行符
要修复代码,请使用fgetc
读取字符,而不要使用strtok
int main( void )
{
FILE *pFile;
if ( (pFile = fopen("test5.txt","r")) == NULL )
exit( 1 );
int c, count;
count = 0;
while ( (c = fgetc(pFile)) != EOF ) {
if ( c == '\n' )
putchar( ' ' );
else
putchar( c );
count++;
if ( count == 60 ) {
putchar( '\n' );
count = 0;
}
}
putchar( '\n' );
fclose(pFile);
}
这是我测试的文件
a dog chased
a cat up the
tree. The cat
looked at the dog from
the top of the tree.
好了
int main() {
FILE *pFile = fopen("test.txt","r");
int c, counter = 0;
if (pFile != NULL) {
while ((c = getc(pFile)) != EOF) {
counter++;
if (c != '\n') { putchar(c); }
if (counter == 60) {
counter = 0;
printf("\n");
}
}
}
fclose(pFile);
}
#包括
#包括
int main(void){//未使用的argv
文件*pFile;
int ch,prev=0;
int totalCharLength=0;
int wrap_size=60;
//字符换行符[]=“换行符”;
pFile=fopen(“test5.txt”,“r”);
if(pFile!=NULL){
而((ch=fgetc(pFile))!=EOF){
if(isspace(ch))
ch='';
如果(上一个=“”&&ch=“”)
继续;//统一
prev=ch;
putchar(ch);
如果(++totalCharLength==wrap_size-1){/-1:包含换行符
putchar('\n');//printf(“%s”,换行符);
totalCharLength=0;
}
}
fclose(pFile);//放入if块中
}
返回0;
}
0)int totalCharLength
未初始化,需要重置。@BLUEPIXY是的,我知道每次达到60,我都必须将totalCharLength重置为0,但这并不能解决我的主要问题。totalCharLength“跳过”超过60。逐个输入并逐个计数。我在文章末尾这样做了。当我一个一个地输入,一个一个地数数的时候。但是,输出并非全部在一行中。如果有60个字符,则只有一次应该有新行。这将产生一个14个字符的新行。您使用的strtok是可疑的。如果使用不当,函数本身使用起来很危险,因为它会遍历传入的字符串,从不重置指针。我怀疑你在乱涂乱画。以这个问题为例,尝试重写。因此,我将x添加到新数组中,然后在计数字符时打印出数组的所有元素?您将x添加到新数组中。然后,一旦数组中填充了所有字符(减去新行字符)。您可以创建一个单独的for循环,一个接一个地打印所有字符,当您达到60时,打印一个新行。继续,直到数组中没有其他字母。使用“我的描述”中的输入文本文件,它只输出树顶部的“查看的do”。感谢您的解释。但是它仍然有一个奇怪的输出。
a dog chased
a cat up the
tree. The cat
looked at the dog from
the top of the tree.
int main() {
FILE *pFile = fopen("test.txt","r");
int c, counter = 0;
if (pFile != NULL) {
while ((c = getc(pFile)) != EOF) {
counter++;
if (c != '\n') { putchar(c); }
if (counter == 60) {
counter = 0;
printf("\n");
}
}
}
fclose(pFile);
}
#include <stdio.h>
#include <ctype.h>
int main(void){//unused argv
FILE *pFile;
int ch, prev = 0;
int totalCharLength = 0;
int wrap_size = 60;
//char newline[] = "newline";
pFile = fopen("test5.txt","r");
if(pFile != NULL){
while((ch = fgetc(pFile)) != EOF){
if(isspace(ch))
ch = ' ';
if(prev == ' ' && ch == ' ')
continue;//unified
prev = ch;
putchar(ch);
if(++totalCharLength == wrap_size -1){//-1 : include newline
putchar('\n');//printf("%s", newline);
totalCharLength = 0;
}
}
fclose(pFile);//put inside if-block
}
return 0;
}