Failed to read struct data from struct File #25395
-
As an exercise,I just want to learn console input/out writing/reading typed data via file.Mainly used os.write_struct[T] and os.read_struct[T]. Help would be match appreciated. running information:
source:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Print the
It calls
Just open new file via mut file2 := os.open(path2)!
file2.read_struct[Address](mut addr) or {
println('Failed to read struct data from file: ${err} ${typeof(err).name}')
return
}
|
Beta Was this translation helpful? Give feedback.
-
Your final println('adress:{$addr') it should be println('adress:${addr}') There's really no reason to take the address of Here's how I changed your example: module main
import os
import strconv
struct Address {
pub:
street string
city string
zip int
}
fn make_address() Address {
print('Enter a street:')
street := os.get_line()
print('Enter a city:')
city := os.get_line()
print('Enter a zip code:')
zipstr := os.get_line()
zip := strconv.atoi(zipstr) or {
println('Failed to make a number')
return Address{}
}
return Address{
street: street
city: city
zip: zip
}
}
fn main() {
path2 := 'address.txt'
mut addr := make_address()
print(addr)
println('-----------------------')
mut file1 := os.create(path2)!
file1.write_struct(addr)!
file1.close()
println('Struct file created successfully')
println('Clearing struct for read')
addr = Address{}
println(addr)
file1 = os.open(path2)!
println('File opened for process---')
file1.read_struct(mut addr) or {
println('Failed to read struct data from file')
return
}
println(addr)
} |
Beta Was this translation helpful? Give feedback.
-
Thank you all for the help!! |
Beta Was this translation helpful? Give feedback.
Your final
println
has bad syntax for printing theaddr
value. Instead ofit should be
There's really no reason to take the address of
addr
and assign it toaddress
, either.Here's how I changed your example: