Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:58:15+00:00 2026-05-31T00:58:15+00:00

Im trying to build a string to call a script with args, and one

  • 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!!!!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:58:18+00:00Added an answer on May 31, 2026 at 12:58 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
I need to get a particular version string from a file (call it version.lst)
I'm trying to build python-kerberos on AIX. kerberospw.c uses a call to asprintf, but
Currently trying to build a script utilizing cmdlets from the MS released Team Foundation
I'm trying to build up a string array in JavaScript and get the results
Im trying to build call to action button on my site using jQuery. I
I have this C++ file (let's call it main.cpp ): #include <string> #include main.y.c
I am trying to call MavenCli from my OSGi plugin project. I have added
I’m trying to build a command string based to pass in a -e flag
In my matlab m-file I am using some logic (string concat) to build variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.