Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslash and doublebackslash

Tags:

c

I've got a file with paths . But I can't read them correctly in C . an example of a line in the file :

C:\Trust\The\process.txt

and I want to have this :

C:\\Trust\\The\\process.txt

But how can I replace antislash by double antislash ? I've got this function :

/*  Replace a string */
char* replace(char* text, char* replace, char* element) {
  int i, j, k;

  int searchSize = strlen(text);
  int replaceSize = strlen(replace);
  int size = strlen(element);

  char* ret;

  if (!searchSize) {
    ret = malloc(size + 1);
    for (i = 0; i <= size; i++) {
      ret[i] = element[i];
    }
    return ret;
  }

  int retAllocSize = (strlen(element) + 1) * 2; 
  ret = malloc(retAllocSize);

  int bufferSize = 0; 
  char* foundBuffer = malloc(searchSize); 

  for (i = 0, j = 0; i <= size; i++) {
    if (retAllocSize <= j + replaceSize) {
      retAllocSize *= 2;
      ret = (char*) realloc(ret, retAllocSize);
    }

    else if (element[i] == text[bufferSize]) {
      foundBuffer[bufferSize] = element[i];
      bufferSize++;


      if (bufferSize == searchSize) {
        bufferSize = 0;
        for (k = 0; k < replaceSize; k++) {
          ret[j++] = replace[k];
        }
      }
    }

    else {
      for (k = 0; k < bufferSize; k++) {
        ret[j++] = foundBuffer[k];
      }
      bufferSize = 0;

      ret[j++] = element[i];
    }
  }


  free(foundBuffer);

  return ret;
}

I thought I could use like this , but it doesn't work :

char *token ;
char s[]="C:\Trust\The\process.txt";
token=replace("\0x5c","\\",s); 

2 Answers

Pulling comments together, you need to understand that the backslash in a string in C source code is an escape charater. It means "the next character has a special meaning".

In order to put a single backslash character in a C string string, you must tell the compiler that "this backslash you must put in the string" and to do that, you put two backslashes in the string in your source code so the string in the compiled code will have a single backslash. In summary:

char s[]= "C:\\my\\dir"";

in your source code, will have a string in the compiled code:

C:\my\dir
like image 75
Paul Ogilvie Avatar answered Feb 20 '26 07:02

Paul Ogilvie


If you're reading from an input file input.txt and each filename ends with a newline, this should work:

#define MAX_LINE_LEN 1024
int main(int argc, char *argv[])
{
  /* File read variables */
  FILE *fp;
  char buf[MAX_LINE_LEN];
  char *token;

  /* Open input file */
  fp=fopen(argv[1], "r");
  if(fp == NULL)
  {
    fprintf(stderr, "Unable to open input file.  Exiting...");
    return 1;
  }

  /* Get each line and print result */
  while ((fgets(buf, sizeof(buf), fp)) != NULL) {
    token=replace("\\", "\\\\", buf); 
    printf("%s", token);
  }

  fclose(fp);
  return 0;
}

Input: input.txt:

C:\Trust\The\process.txt

Output:

C:\\Trust\\The\\process.txt
like image 20
Andy J Avatar answered Feb 20 '26 05:02

Andy J