当我不想要它时,fgets拆分输入字符串(fgets splits input string when I dont want it to)

编程入门 行业动态 更新时间:2024-10-06 06:53:20
我不想要它时,fgets拆分输入字符串(fgets splits input string when I dont want it to)

我正在使用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.

更多推荐

本文发布于:2023-07-09 10:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   字符串   fgets   splits   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!