RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[io]

Martin Hope
Ziaft
Asked: 2022-07-10 16:39:47 +0000 UTC

写入和读取二进制文件 (C++)

  • 0

有必要将有关机器的信息写入文件,并能够补充该文件并读取一定数量的记录。从二进制文件读取时,输出为:

    Automobile 1�犎�
                    Brand: dasf0�t�UMileage: asdf芎�
                Price: asdf 芎�
                               Automobile 2�犎�Brand: afdsfdas0�t�U`�t�UMileage: dafs芎�
                Price: fdas

(对于字段值中无意义的一组字母,我深表歉意)。据我了解,新行的转义序列在某种程度上被错误地解释了。对于录制,我使用以下功能:

void binary_write(std::ofstream& ofs, std::string& s){                                                             
     ofs.write((char*)&s, sizeof(s));                                                                               
 }

这是我写的几行:

first_line = "Automobile " + std::to_string(count+1);                                                      
         brand = "Brand: " + brand;                                                                                 
         manufacture = "Manufacture: " + manufacture;                                                               
         release = "Year of release: " + release;                                                                   
         mileage = "Mileage: " + mileage;                                                                           
         price = "Price: " + price;

阅读代码:

while(ifs.read(&temp, sizeof(char)))                                                               
                       std::cout << temp;

谁能建议如何使用二进制文件正确组织 I/O?提前致谢。

c++ io
  • 1 个回答
  • 33 Views
Martin Hope
true-hacker
Asked: 2020-05-07 10:30:39 +0000 UTC

Haskell 和 Monads

  • 1

播放这段代码时:

half x = if even x
           then Just (x `div` 2)
           else Nothing

printMaybe m = case m of
  Nothing -> putStrLn "List was empty!"
  Just x -> print x

main = do
  let halfN = Just 4 >>= half
  halfN >>= printMaybe

发生错误:

main.hs:9:1: error:
    * Couldn't match expected type `IO t0' with actual type `Maybe ()'
    * In the expression: main
      When checking the type of the IO action `main'
  |
9 | main = do
  | ^
main.hs:11:13: error:
    * Couldn't match type `IO' with `Maybe'
      Expected type: Maybe a0 -> Maybe ()
        Actual type: Maybe a0 -> IO ()
    * In the second argument of `(>>=)', namely `printMaybe'
      In a stmt of a 'do' block: halfN >>= printMaybe
      In the expression:
        do let halfN = Just 4 >>= half
           halfN >>= printMaybe
   |
11 |   halfN >>= printMaybe

这是解决此问题的一种方法:

half x = if even x
           then Just (x `div` 2)
           else Nothing

printMaybe m = case m of
  Nothing -> putStrLn "List was empty!"
  Just x -> print x

main = do
  let halfN = Just 4 >>= half
  printMaybe $ halfN

我无法弄清楚为什么原始程序不起作用。首先,我们得到函数中数字的一半half(以 monad 的形式Maybe),然后我们将这个值“推”到输出函数 -printMaybe中。

为什么原始版本的代码不起作用?

io
  • 1 个回答
  • 10 Views
Martin Hope
Suifuto
Asked: 2020-05-16 13:57:53 +0000 UTC

Haskell 控制台逐行输出

  • 0

有一个功能,我需要逐行输出

x y результат

所有没有更多的字符,如何做到这一点,以便 this ... 通常输出到每一行而没有额外的字符到控制台。

唯一的选择,但我发现只有一个论点

main = print([f1 x1 0 | x1 <- [0.0,0.1..10.0]])
io
  • 2 个回答
  • 10 Views
Martin Hope
mend4x
Asked: 2020-03-05 20:27:02 +0000 UTC

用fortran将数据写入文件

  • 1

再会。编写程序并运行它。该程序本身没有给出任何错误,它似乎可以工作。创建文件但不向文件写入任何内容。两个程序的行为已经相同。这是一个

program ioposneg
implicit none
integer::i,a,x
do i=1,10
  open(10,file='posneg')
  read(10,*) a
  if (a>0) then
    open(15,file='pos')
    x=a
    write(15,*) x
  else 
    open(20,file='neg')
    x=a
    write(20,*) x
  end if
end do
print*,'finished'
end program ioposneg

这是第二个

program ioformat
implicit none
real::x=0,ex
integer::i
character(len=5)::c='x',ec='ex'
open(10,file='xex')
write(10,2) c,ec
2 format(2a10)
do i=1,11
  ex=x**2
  open(10,file='xex')
  write(10,5)x,ex
  5 format(2f10.5)
  x=x+0.1
end do
print*,'finished'
end program ioformat

第一个程序的可读文件如下所示:

2
3
-9
15
-25
-47
85
68
-10
-5
io
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5