Question

I don't know the title correctly addresses my problem or not. So, I will just go with it. Here is the problem, I have to input a char array of a file path (in Windows) containing lots of backslashes in it, eg. "C:\myfile.txt" and return an unsigned char array of C-style file paths, eg. "C:\myfile.txt".

I tried to write a function.

unsigned char* parse_file_path(char *path);
{
   unsigned char p[60];
    int i,j;
    int len = strlen(path);
    for(i=0,j=0; i<len; i++, j++)
    {
        char ch = path[i];
        if(ch==27)
        {

            p[j++]='\\';
            p[j]='\\';
        }
        else
            p[j] = path[i];
    }
    p[j]='\0';
    return p;
}

The weird thing (for me) I am encountering is, here path contains only one backslash '\'. In order to get one backslash, I have to put '\' in path. This is not possible, cause path cannot contain '\'. When I call it like this parse_file_path("t\es\t \it), it returns t←s it. But parse_file_path("t\\es\\t \\it") returns t\es\t \it.

How can I accomplish my task? Thanks in advance.

Was it helpful?

Solution

If I can just mention another problem with your code.

You are returning a local variable (your unsigned char p). This is undefined behavior. Consider declaring a char* p that you assign memory to dynamically using malloc and then returning p as you do. E.g. something like:

char* p = malloc(60);

A common practice is to use sizeof when allocating memory with malloc but here I've passed 60 directly as the C standard guarantees that a char will be 1 byte on all platforms.

But you have to free the memory assigned with malloc.

Or alternatively, you can change the function to take a buffer as an input argument that it then writes to. That way you can pass a normal array where you would call this function.

Regarding your slashes issue, here:

p[j++]='\\';
p[j]='\\';

Position j in p will be changed to \\, then j will be incremented and at the very next line you do the same for the succeeding char position. Are you sure you want the two assignments?

By the way if you are inputting the path from the command line, the escaping will be taken care of for you. E.g. consider the following code:

#include <stdio.h>
#include <string.h> /* for strlen */
#include <stdlib.h> /* for exit */

int main() 
{
    char path[60];

    fgets(path, 60, stdin); /* get a maximum of 60 characters from the standard input and store them in path */

    path[strlen(path) - 1] = '\0'; /* replace newline character with null terminator */

    FILE* handle = fopen(path, "r");

    if (!handle)
    {
        printf("There was a problem opening the file\n");
        exit(1); /* file doesn't exist, let's quite with a status code of 1 */
    }

    printf("Should be good!\n");

    /* work with the file */

    fclose(handle);

    return 0; /* all cool */
}

And then you run it and input something like:

C:\cygwin\home\myaccount\main.c

It should print 'Should be good!' (provided the file does exist, you can also test with 'C:\').

At least on Windows 7 with cygwin this is what I get. No need for any escapes as this is handled for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top