我试着去建立一个字符串时,得到一个lineshift [英] Im getting a lineshift when trying to build a string
问题描述
我试着去建立一个字符串调用带有ARGS一个脚本,args来一个从文件中读取,但即时得到一个lineshift和输出是这样的。
/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh xogs1a 3/37
> lastConfig.txt
我要的3/37和> lastConfig是在同一行。
这是我的code。
字符getConfig [100] =/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh;
焦炭filedumpto [50] => lastConfig.txtFILE *文件=的fopen(rport.txt,R);
如果(文件== NULL)
{
返回NULL;
}fseek的(文件,0,SEEK_END);
长整型大小= FTELL(文件);
倒带(文件);
字符*端口=释放calloc(大小,1);
FREAD(端口,1,大小,文件);strcat的(getConfig,ARGV [1]);
strcat的(getConfig,口);
strcat的(getConfig,filedumpto);的printf(getConfig);//系统(getConfig);返回0;
修改
我甩输出到一个文件,并在VIM打开它,看,它的变量,这是进入我相信后发送^ M?它为什么要这样做IV尝试下这个职位的解决方案,但它不工作。
测试端口的打印!!!!
/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh randa1ar248分之5^ M
> SisteConfig.txt
测试端口的打印!!!!
该文件可能与结尾的尾行序列。
单薄,脆弱的解决方案:
FREAD(端口,1,大小-1文件); //如果它只是一个CR或LF
FREAD(端口,1,大小2,文件); //如果它是CRLF的组合。
//你的code继续在这里
有一个更好的,可移植的解决方案会做这样的事情:
的char *端口=释放calloc(大小+ 1的sizeof(字符)); //确保字符串用null结束
INT LEN = FREAD(端口,1,大小,文件); //读取len个字符
字符*结束=口+ LEN - 1; //从文件中最后一个字符//如果最后一个字符为CR或LF,缩短字符串。
而(完> = P)及和放大器; ((*年底=='\\ r')||(*年底=='\\ n')){
*(end--)='\\ 0';
}
下面的工作code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;炭getConfig [100] =/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh;
为const char * filedumpto => lastConfig.txtINT主(CHAR ARGC,CHAR *的argv []){
FILE *文件=的fopen(rport.txt,R);
如果(文件== NULL){
返回1;
} fseek的(文件,0,SEEK_END);
长整型大小= FTELL(文件);
倒带(文件); 字符*端口=释放calloc(大小+ 1,1);
INT LEN = FREAD(端口,1,大小,文件); //读取len个字符
字符*结束=口+ LEN - 1; //从文件中最后一个字符 //当最后一个字符为CR或LF,缩短字符串。
而((完> =口)及及((*年底=='\\ r')||(*年底=='\\ n'))){
*(end--)='\\ 0';
} strcat的(getConfig,ARGV [1]);
strcat的(getConfig,口);
strcat的(getConfig,filedumpto); 的printf(%S \\ n,getConfig);
返回0;
}
Im trying to build a string to call a script with args, and one of the args is read from a file but im getting a lineshift and the output is like this
/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh xogs1a 3/37
> lastConfig.txt
i want the 3/37 and > lastConfig to be on the same line.
this is my code.
char getConfig[100] = "/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh ";
char filedumpto[50] = " > lastConfig.txt";
FILE* file = fopen("rport.txt","r");
if(file == NULL)
{
return NULL;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
rewind(file);
char* port = calloc(size, 1);
fread(port,1,size,file);
strcat(getConfig, argv[1]);
strcat(getConfig, port);
strcat(getConfig, filedumpto);
printf(getConfig);
//system(getConfig);
return 0;
edit
i dumped the output to a file and opened it in vim to see and it sends a ^M after the variable, which is enter i believe? why does it do this iv tried the solutions under this post but its not working.
tester port print!!!!
/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh randa1ar2 5/48^M
> SisteConfig.txt
tester port print!!!!
The file probably ends with an end-of-line sequence.
Sleazy, brittle solution:
fread(port, 1,size-1, file); // If it's just a CR or LF
fread(port, 1,size-2, file); // If it's a combination of CRLF.
// your code continues here
A better, portable solution will do something like this:
char *port = calloc(size+1, sizeof(char)); // Ensure string will end with null
int len = fread(port, 1, size, file); // Read len characters
char *end = port + len - 1; // Last char from the file
// If the last char is a CR or LF, shorten the string.
while (end >= p) && ((*end == '\r') || (*end == '\n')) {
*(end--) = '\0';
}
Here's working code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char getConfig[100] = "/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh ";
const char *filedumpto = " > lastConfig.txt";
int main(char argc, char *argv[]) {
FILE *file = fopen("rport.txt", "r");
if (file == NULL) {
return 1;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
rewind(file);
char *port = calloc(size+1, 1);
int len = fread(port, 1, size, file); // Read len characters
char *end = port + len - 1; // Last char from the file
// While the last char is a CR or LF, shorten the string.
while ((end >= port) && ((*end == '\r') || (*end == '\n'))) {
*(end--) = '\0';
}
strcat(getConfig, argv[1]);
strcat(getConfig, port);
strcat(getConfig, filedumpto);
printf("%s\n", getConfig);
return 0;
}
这篇关于我试着去建立一个字符串时,得到一个lineshift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!