#Create file in tmp directory

48 messages · Page 1 of 1 (latest)

chrome pulsar
#

I want to ask how I can achieve this?

I tried this std::fs::write("/tmp/iqbal.txt", "iqbal");, but does not seem to work

fs::create_dir_all("/tmp/iqbal.txt"); also does not seem to be working

How can I create a file in a specific path, thank you

ancient harness
#

you can see it with std::fs::write("/tmp/iqbal.txt", "iqbal").unwrap(); for a quick and dirty method

chrome pulsar
#

it does not return an error, but when I want to access it, it is not there

#

its like it is not creating that file

ancient harness
#

?eval ```rs
let name = "/tmp/iqbal.txt";
std::fs::write(name, "iqbal").unwrap();
std::fs::read_to_string(name)

bold boughBOT
#
     Running `target/debug/playground`

Ok("iqbal")
ancient harness
#

there must be something else going on

chrome pulsar
ancient harness
#

?eval ```rs
let name = "/tmp/iqbal.txt";
std::fs::write(name, "iqbal").unwrap();
let first = std::fs::read_to_string(name);
std::fs::write(name, "abcdef").unwrap();
let second = std::fs::read_to_string(name);
(first, second)

bold boughBOT
#
     Running `target/debug/playground`

(Ok("iqbal"), Ok("abcdef"))
chrome pulsar
# ancient harness yes

if lets say it did not overwrite, do u know what is the cause? thank you for helping btw

ancient harness
#

I'm leaning towards that this is a problem with your diagnosis

#

although it's possible for the file content to not have been flushed to the actual file system

#

it should eventually be there though

#

something else could be writing to the file too

chrome pulsar
#

let me try tell you want I actually want to do

I want to create a file at dir /tmp, after that I overwrite the content of the file

ancient harness
#

?crate tempdir

bold boughBOT
#

A library for managing a temporary directory and deleting all contents when it's
dropped.

Version

0.3.7

Downloads

12 557 139

ancient harness
#

this is one crate that's useful for this use case

#

but you still have to write the files yourself

#

what you've shown didn't have anything indicating that it wouldn't work

#

although now that I see it, you tried to make a directory called *.txt

#

maybe that was confusing?

#

if you make a directory with a name, you can't make a file with the same name

#

?eval ```rs
let name = "/tmp/iqbal.txt";
std::fs::create_dir_all(name).expect("should create dir");
std::fs::write(name, "iqbal").expect("should write to file");
std::fs::read_to_string(name)

bold boughBOT
#
     Running `target/debug/playground`
thread 'main' panicked at 'should write to file: Os { code: 21, kind: IsADirectory, message: "Is a directory" }', src/main.rs:4:31
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
chrome pulsar
#

thread 'main' panicked at 'should write to file: Os { code: 31, kind: Uncategorized, message: "Is a directory" }' what does this mean?

ancient harness
#

it's a Debug representation of the error

chrome pulsar
#

it means this one is created by someone? and I need to ask the person who create this error message?

ancient harness
#

what?

chrome pulsar
ancient harness
#

the error message itself tells you that there's already a directory with that name

#

so you can't write to it

#

you can only write to files

#

do you understand what all the code snippets I showed do?

chrome pulsar
#

i think I do, it first create a directiry, then it write to that file, and then read from that file correct?

ancient harness
#

the last snippet yes

chrome pulsar
#

i see

#

thread 'main' panicked at 'should write to file: Os { code: 28, kind: InvalidInput, message: "Invalid argument" }',

#

do you know what this means

ancient harness
#

or it could be something else

#

?eval ```rs
let name = "/tmp/@#$&_-()=%*':!?+%@'?.txt";
std::fs::write(name, "iqbal").unwrap();
std::fs::read_to_string(name)

bold boughBOT
#
     Running `target/debug/playground`

Ok("iqbal")
ancient harness
#

lol ok I can't find an invalid character