user7889665
user7889665

Reputation:

Backslash and doublebackslash

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

Upvotes: 3

Views: 166

Answers (4)

chqrlie
chqrlie

Reputation: 144780

There is some confusion in your problem statement:

The variable definition char s[]="C:\Trust\The\process.txt"; is incorrect. It should be written:

char s[] = "C:\\Trust\\The\\process.txt";

The compiler interprets the \ character in a string literal as an escape character to encode special and non-printing characters. The \\ represents a single \ character in the string.

The file contents should not need to have its \ characters doubled or escaped in any way. Unless you perform some sort of parsing when you read the file, the characters read will be stored untouched in memory and the sequence C:\Trust\The\process.txt in the file will be read as a string identical to s.

Upvotes: 0

nicomp
nicomp

Reputation: 4647

When you do this:

char s[]="C:\Trust\The\process.txt";

your backslash is gone at compile time. The \T becomes a tab, for example. When you call

token=replace("\0x5c","\\",s); 

the contents of s has already been 'edited' by the compiler and the backslashes are gone.

Your test case needs to be

char s[]="C:\\\\Trust\\\\The\\\\process.txt";

and when you call the replace function you will have the single backslashes in s.

Upvotes: 1

Andy J
Andy J

Reputation: 1545

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

Upvotes: 1

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

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

Upvotes: 1

Related Questions