从文件中读取跳过空格或空行文本 [英] Read text from file skipping any spaces or empty lines
问题描述
嗨。
我需要阅读一些文件,而无需characters.They之间的空格或空行可以有不同的布局,比如为1.txt,2.txt或3.txt:
的 1.txt的:的
T
H
一世
小号
F
一世
升
Ë
的 2.txt:的
→一世ķËŧH一个ŧ
的 3.txt:的
S 0 M E牛逼H I体中
喜欢
ŧ小时的牛逼
如何做呢?我有以下的code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释主(){
焦炭℃;
FILE * FP; FP = FOPEN(1.txt的,R);
如果(FP == NULL){
看跌期权(打开文件失败\\ n);
出口(-1);
} 而(的fscanf(FP,%C \\ n,&安培;!C)= EOF){
/ *做的事情与C VAR * /
} FCLOSE(FP);
出口(0);
}
我觉得你问如何在一个文本文件阅读,忽略空格和空行。请澄清你的问题,使我们可以帮助你更好的。
下面的东西,让你开始:
您可以在整个文件中读取,然后删除你不想要的东西。以下后凭我的经验同意您的应用程序可能会运行得更快这种方式:
读取文件用C 更快
如果您使用这种方法达成一致,这下一篇文章将告诉你如何读取文本文件到缓冲区中,有一些错误处理:
<一href=\"http://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c\">Reading整个文本文件转换成字符数组用C
然后你可以使用像这样把空格去掉,并为一些额外的新行相似:
<一href=\"http://stackoverflow.com/questions/13084236/function-to-remove-spaces-from-string-char-array-in-c\">Function去除字符串/字符数组空间用C
我希望这有助于让你开始,也许有人会找时间今天发布一些code你。
Hi. I need to read several files without spaces or empty lines between characters.They can have different layouts, such as 1.txt, 2.txt or 3.txt:
1.txt:
t
h
i
s
f
i
l
e
2.txt:
l
i
k
e
t
h
a
t
3.txt:
s o m e t h i n g
l i k e
t h a t
How do i do that ? I Just have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char c ;
FILE *fp;
fp = fopen("1.txt","r");
if(fp == NULL){
puts("Open file failed\n");
exit(-1);
}
while(fscanf(fp,"%c\n",&c)!=EOF){
/*do things with c var*/
}
fclose(fp);
exit(0);
}
I think you're asking how to read in a text file, ignoring spaces and empty lines. Please clarify your question so that we can help you better.
Here's something to get you started:
You could read in the entire file, then remove anything you don't want. The following post agrees with my experience that your application will probably run faster that way: Reading a file faster in C
If you agree with that approach, this next post will show you how to read the text file into a buffer, with some error handling: Reading the whole text file into a char array in C
Then you can use something like this to remove the spaces, and something similar for the extra new lines: Function to remove spaces from string/char array in C
I hope this helps get you started, and perhaps someone will find time today to post some code for you.
这篇关于从文件中读取跳过空格或空行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!