English 中文(简体)
查阅在C和Java撰写的档案
原标题:Read from a file written in C with Java

我在C处有一份双手文件,我希望在java读。 我在C文中写道:

void Log(TCHAR *name,int age){
    write_int(file, 2012);
    write_int(file, 4);
    write_int(file, 16);
    write_int(file, 12);
    write_int(file, 58);
    write_int(file, 50);

    fwrite(&name, sizeof(name), 1, file);
    fwrite(&age, sizeof(age), 1, file);
    fflush(processusFile);
}

Java http://www.heatonresearch.com/code/22/BinaryFile.java"rel=“nofollow”http://www.heatonresearch.com/code/22/BinaryFile.java。

RandomAccessFile f = new RandomAccessFile("myfile.dat", "r");
BinaryFile binaryFile = new BinaryFile(f);
ArrayList<String> text = new ArrayList<>();
while (true) {
try {
    Calendar calendar = new GregorianCalendar();
    int year = (int) binaryFile.readDWord();
    int month = (int) binaryFile.readDWord();
    int date = (int) binaryFile.readDWord();
    int hourOfDay = (int) binaryFile.readDWord();
    int minute = (int) binaryFile.readDWord();
    int second = (int) binaryFile.readDWord();

    calendar.set(year, month, date, hourOfDay, minute, second); 
    System.out.println(binaryFile.readFixedString(64));    
    catch (Exception e) {
        break;  
    }
}

这不是为果园而工作,而是为果园而工作。 我怎么能写一下强势?

最佳回答

这是因为sizeof(姓名)char/code> Pointer on their system,not the length of the string. 你们也需要在间隔时间之外单独写出。

size_t len = strlen(name);
write_int(file, len);
fwrite(&name, len, sizeof(char), file);
问题回答

这是错误的:

fwrite(&name, sizeof(name), 1, file);

这只能写出与pointer/strong>的大小相当的一些特性,通常为4。

If the size you want to write out is fixed (looks like the Java code expects 64 characters), you need to pass that size somehow to Log().

在C中,没有任何东西可以说明(已经通过)从职能范围内作为第一种论点的阵列(< 代码>)的范围,这就是为什么你必须明确表示这一点。

你们必须接受“护理”。

java的胎面总为32比方,而在C区则不是这样。 在C中,该比例为16,32,64比值。

页: 1

fwrite(&name, sizeof(name), 1, file);

你们实际上想要的是:

fwrite(name, sizeof(TCHAR), strlen(name) + 1, file);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...