亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從用 Java 編寫的文件中讀取 C 中的數據(二進制或文本)?

如何從用 Java 編寫的文件中讀取 C 中的數據(二進制或文本)?

HUX布斯 2022-11-02 16:09:40
用Java編寫:File file = new File("abc");try(FileOutputStream fos = new FileOutputStream(file)) {    FileChannel outChannel = fos.getChannel();    ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES*3).order(ByteOrder.nativeOrder());    buffer.putInt(154);    buffer.putint(98);    buffer.putInt(6);    outChannel.write(buffer);    IntStream.rangeClosed(1,3).forEach((iter) -> {        String reqString = (Integer.toString(iter) + "_string");        byte[] bytes = reqString.getBytes(Charset.forName("US-ASCII"));        ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length).order(ByteOrder.nativeOrder());        byteBuffer.put(bytes);        try {            outChannel.write(byteBuffer);        }        catch(IOException ex) {            ex.printStackTrace();        }    });}   catch(IOException ex) {    ex.printStackTrace();}用 C 讀: int main() { int one,two,three; 文件 *fp = fopen("abc","r+");fscanf(fp,"%d",&one);fscanf(fp,"%d",&two);fscanf(fp,"%d",&three);printf("%d %d %d",one,two,three);char ch;fread(&ch,sizeof(char),1,fp);while(ch!= '\0') {    printf("%c",ch);    fread(&ch,sizeof(char),1,fp);} return 0;}輸出為:我不明白我看到了什么。有三個值,沒有一個是我寫的。它們是內存位置嗎?還是垃圾值?Java以哪種格式寫入文件。因為,C 以底層平臺的本機順序讀取,所以我嘗試使用 ByteBuffer。如果我使用 DataOutputService,我認為 Java 以“BIG_ENDIAN”格式寫入,而 C 可能無法讀取正確的值。另外,在這種情況下如何讀寫字符串。Java,默認情況下使用 UTF-8,因此,我沒有直接使用 FileOutputStream,而是將它們轉換為 ASCII 格式的字節,以便我可以逐個字符地讀取它們。但是,沒有顯示輸出。我什么時候可以使用fscanf(fp,"%s",string) (字符串是分配了足夠內存的 char 數據類型變量)*?理想的解決方案是 Java 能夠以特定順序寫入數據,而 C 能夠使用 讀取它fscanf(),因此更容易識別整數/浮點數/字符串。
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

我的 Java 有點生疏,但看起來你寫入文件的內容是三個 4 字節整數,按順序和機器字節順序(所以可能是小端序)。這意味著您的文件應該具有(十六進制):

9a 00 00 00 62 00 00 00 06 00 00 00

但是您的掃描預計會將數字視為以空格分隔的文本,例如(以十六進制表示)

31 35 34 20 39 38 20 36 0a

你可能應該使用類似的東西fread(),它不做解析:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

并且目標應該是類似的地址int[3]。


查看完整回答
反對 回復 2022-11-02
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

我知道fscanf讀取一個字符流,然后根據給定的格式對其進行解析。這意味著我們應該以 C 支持的字符格式用 Java 編寫文件。


這就是我所做的:


Java代碼:


package scratchpad;


import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.nio.charset.StandardCharsets;

import java.util.logging.Level;

import java.util.logging.Logger;


public class WriteClass {

    void writeFunction() {

        File defgFile = new File("/home/thor/Documents/ScratchPad/def.bin");

        try(FileOutputStream fos = new FileOutputStream(defgFile)){

            BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(fos,StandardCharsets.US_ASCII));

            bos.write(Integer.toString(123));

            bos.newLine();

            bos.write(Integer.toString(96));

            bos.newLine();

            bos.write(Integer.toString(1));

            bos.newLine();

            bos.write("Water");

            bos.newLine();

            bos.write(Integer.toString(2));

            bos.newLine();

            bos.write("Forest");

            bos.newLine();


            bos.flush();

            bos.close();

        }

        catch(IOException ex) {

            Logger.getLogger(WriteClass.class.getName()).log(Level.SEVERE,"Can't open a new file",ex);

        }

    }

}

需要注意的重要一點是,我曾經OutputStreamWriter以 ASCII 格式編寫文本文件。另一點是我們不必擔心ByteOrder將 ASCII 值寫入文件的位置。似乎Java正在處理它。


獨立于平臺的bos.newLine()方式來編寫一個新行。 bos.flush()是強制性的,否則數據將不會被寫入。


C代碼:


#include <stdio.h>

#include <stdlib.h>

#include <math.h>   


int main() {


    FILE *fp = fopen("def.bin","r");

    int one,two,three;

    fscanf(fp,"%d",&one);

    fscanf(fp,"%d",&two);

    printf("%d\n%d\n",one,two);


    int index;

    fscanf(fp,"%d",&index);

    char* string;

    fscanf(fp,"%s",string); 

    printf("%d\n%s\n",classno,string);

    return 0;

}

我注意到字符串char*沒有分配內存并且仍然有效。


查看完整回答
反對 回復 2022-11-02
  • 2 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號