# Unable runTest For cat-file command

**URL:** <https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [January 14, 2025, 1:09pm UTC](https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184 "2025-01-14T13:09:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amanuell2](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/amanuell2/32/3697_2.png) [@amanuell2](https://forum.codecrafters.io/u/amanuell2)\
**Post date:** [January 14, 2025, 1:09pm UTC](https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184/1 "2025-01-14T13:09:16Z")

</div>

I’m stuck on Stage #yt5.

I’ve tried to parse the escape character and i work’s fine for most of the test cases but on Writing file “/tmp/quz/f\n48” it’s not passing the file writing part .

Here are my logs:

```auto
[your-program] $ cat "/tmp/quz/f\n48" "/tmp/quz/f\20" "/tmp/quz/f'\'69"
[your-program] cat: /tmp/quz/fn48: No such file or directory
[tester::#YT5] Output does not match expected value.
[tester::#YT5] Expected: "pear strawberry.mango orange.strawberry pineapple."
[tester::#YT5] Received: "cat: /tmp/quz/fn48: No such file or directory"
[your-program] cat: /tmp/quz/f20: No such file or directory
[your-program] cat: /tmp/quz/f''69: No such file or directory
[your-program] $
[tester::#YT5] Assertion failed.
[tester::#YT5] Test failed

```

And here’s a snippet of my code:

```go
func executeCommand(input string) {
	input = strings.ReplaceAll(input, "\\\n", "")

	commands := strings.Trim(input, "\r\n")

	args, err := parseArgs(commands)
.
.
.
}

func parseArgs(input string) ([]string, error) {
	var args []string
	var currentArg strings.Builder
	currentArg.Grow(len(input))
	inSingleQuote := false
	inDoubleQuote := false

	for i := 0; i < len(input); i++ {
		char := input[i]

		switch char {

		case '\'':
			if inDoubleQuote {
				currentArg.WriteByte(char)
			} else {
				inSingleQuote = !inSingleQuote
			}
		case '"':
			if inSingleQuote {
				currentArg.WriteByte(char)
			} else {
				inDoubleQuote = !inDoubleQuote
			}
		case ' ':
			if inSingleQuote || inDoubleQuote {
				currentArg.WriteByte(char)
			} else if currentArg.Len() > 0 {
				args = append(args, currentArg.String())
				currentArg.Reset()
			}
		case '\\':
			if i+1 < len(input) {
				nextChar := input[i+1]
				if nextChar == ' ' {
					currentArg.WriteByte(' ')
				} else {

					currentArg.WriteByte(nextChar)
				}
				i++
			} else {
				currentArg.WriteByte(char)
			}

		default:
			currentArg.WriteByte(char)

		}
	}

	if currentArg.Len() > 0 {
		args = append(args, currentArg.String())
	}

	if inSingleQuote || inDoubleQuote {
		return nil, fmt.Errorf("unmatched quote")
	}

	return args, nil

}

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [January 14, 2025, 1:37pm UTC](https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184/2 "2025-01-14T13:37:53Z")

</div>

Hi @amanuell2, it seems the current code doesn’t differentiate between being inside and outside of quotes:

> $ cat “/tmp/quz/f`\`n48” “/tmp/quz/f\20” “/tmp/quz/f’'69”  
> cat: /tmp/quz/fn48: No such file or directory

Suggestions:

1. Try to differentiate inside and outside.
2. Don’t remove `\` inside quotes.

---

<div class="post-metadata">

**Author:** ![amanuell2](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/amanuell2/32/3697_2.png) [@amanuell2](https://forum.codecrafters.io/u/amanuell2)\
**Post date:** [January 14, 2025, 2:01pm UTC](https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184/3 "2025-01-14T14:01:57Z")

</div>

```auto
case '\\':
// this if case was the thing i missed (big thanks to @andy1li 
			if inDoubleQuote || inSingleQuote {
				currentArg.WriteByte(char)
			}
			if i+1 < len(input) {
				nextChar := input[i+1]
				if nextChar == ' ' {
					currentArg.WriteByte(' ')
				} else {
					currentArg.WriteByte(nextChar)
				}
				i++
			}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [January 22, 2025, 2:44pm UTC](https://forum.codecrafters.io/t/unable-runtest-for-cat-file-command/4184/4 "2025-01-22T14:44:44Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
