我正在使用fgets从文件读取一行,但它将输入行分成多行,这不是我想要它做的。
这是我正在使用的代码:
char string[LMAX]; FILE *fp; fp = fopen("input.txt","r"); while(fgets (string, LMAX, fp) != NULL) { //Reading the file, line by line printf("%s", string); }例如,如果我的输入文件有1,2;0,5;7,8;0,6;; 控制台中的输出将其分为0。
即1,2; 0,5;7,8; 0,6;; 1,2; 0,5;7,8; 0,6;;
我希望它能在一行中打印
编辑:确实这是我的输入文件的问题。
tldr:从不使用记事本
I'm using fgets to read in a line from a file, but it splits up the input line into multiple lines which is not what I want it to do.
This is the code that I'm using:
char string[LMAX]; FILE *fp; fp = fopen("input.txt","r"); while(fgets (string, LMAX, fp) != NULL) { //Reading the file, line by line printf("%s", string); }For example if my input file had 1,2;0,5;7,8;0,6;; the output in the console splits it up at the 0's.
i.e 1,2; 0,5;7,8; 0,6;;
I want it to print in one single line
EDIT: Indeed it was an issue with my input file.
tldr: never use notepad
最满意答案
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ int LMAX = 1024; char foo[LMAX]; FILE *fp; fp = fopen("input.txt","r"); while(fgets (foo, LMAX, fp) != NULL) { //Reading the file, line by line printf("%s", foo); } }在我的系统上一切正常(虽然我正在运行osx)。
1,2;0,5;7,8;0,6;;编码明智,你的逻辑和修饰符是合理的。 我希望输出中的错误是由于Visual Studio中固有的东西。 或者输入文件可能存在问题。
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ int LMAX = 1024; char foo[LMAX]; FILE *fp; fp = fopen("input.txt","r"); while(fgets (foo, LMAX, fp) != NULL) { //Reading the file, line by line printf("%s", foo); } }Everything works fine on my system (though I'm running osx).
1,2;0,5;7,8;0,6;;Coding wise, your logic and modifiers are sound. I expect the error in output is due to something inherent within Visual studios. Or there may be an issue with your input file.
更多推荐
发布评论