Im getting a lineshift when trying to build a string

我们两清 提交于 2019-12-11 13:50:28

问题


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!!!!

回答1:


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;
}



回答2:


The input file ("rport.txt") probably contains a newline. Strip whitespace from the end of the read input, and it should be ok.



来源:https://stackoverflow.com/questions/9635321/im-getting-a-lineshift-when-trying-to-build-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!