是否可以在c / c ++中的double(8字节)类型的变量中存储8个字符(每个1字节)? [英] Is it possible to store 8 characters (1-byte each) in a variable of type double (8-bytes) in c/c++?

查看:195
本文介绍了是否可以在c / c ++中的double(8字节)类型的变量中存储8个字符(每个1字节)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些旧的Fortran77代码迁移到C / C ++。在Fortran77代码中,如果从文件读入8个字符,它们可以存储在real * 8类型的变量中,没有问题。

I am migrating some legacy Fortran77 code to C/C++. In the Fortran77 code, if 8 characters are read in from a file, they can be stored in a variable of type real*8 without a problem.

是否可以在C或C ++中做类似的事情?如果是这样,我该怎么办呢?我没有能够在互联网上找到任何解决方案。我需要使用C / C ++读取8个字符,并将它们存储在double类型的变量中,然后传递回Fortran并对应于原始的真实* 8变量。

Is it possible to do a similar thing in C or C++? If so, how would I do it? I haven't been able to find any solutions on the internet. I need to read in 8 characters using C/C++ and store them in a variable of type double, which is then passed back to Fortran and corresponds to the original real*8 variable.

非常感谢您提供任何帮助。

Many thanks in advance for any help.

编辑:
在回应@sixlettervariables时,我的用例有点多。我对他的建议的问题是,我只知道在运行时每行的格式(即哪些字段是字符串,哪些数字),因此我不知道结构体应该静态的成员。这些字段还需要按照它们被读入的顺序占据连续的存储器块。

In response to @sixlettervariables, I'll just clarify my use-case a bit more. The issue I have with his suggestion is that I only know the format of each line (i.e. which fields are strings, which numbers) at runtime, and hence I can't know what members the struct should have statically. The fields also need to occupy a contiguous block of memory in the order they are read in.

具体地,在程序的一次运行中,每一行的格式可以是: f1:string,f2:string,f4:number,f5:number 强>。对于第一种情况,我需要:

Concretely, in one run of the program the format of each line might be: f1:string, f2:number, f3:number, f4:string, but in another f1:string, f2:string, f3:string, f4:number, f5:number. For the first case I'd need:

struct { char[8] f1; double f2; double f3; char[8] f4}

对于第二个,我需要:

struct { char[8] f1; char[8] f2; char[8] f3; double f4; double f5} 

也许有一些方法可以用模板做到这一点?

Perhaps there is some way to do this with templates?

推荐答案

您不需要将它们存储在 double ,因为Fortran需要这样做。事实上,你绝对不应该在你的C / C ++代码中这样做。

You do not need to store them in a double just because Fortran needed to do that. In fact, you absolutely should not do that in your C/C++ code.

只需将字符数据存储在字符数组中。

Simply store the character data in a character array.

如果要混合Fortran和C / C ++ ,两者在他们的ABI之外不知道彼此。从C侧,你可以简单地声明 Fortran接口需要一个字符数组,而实际上它期望一个双精度数组。

If you're mixing Fortran and C/C++, the two have no idea about one another outside of their ABI. From the C side you can simply claim that the Fortran interface takes a character array, when in fact it is expecting an array of doubles. And the same is true from the Fortran side.

从C侧:

extern void FCHARS(char* str, int length);

/* ... */
int flength = 0; /* optional: length of the string in Fortran terms */
char str[9]; /* in C/C++ add one for \0 at the end */

/* read in up to a block of 8 */
fgets(str, sizeof(str), fp);

/* At this point if you know the 8 characters are space padded to their end
 * you have no more work to do, otherwise you may need to fill from the right
 * with spaces.
 */
{
    size_t ii = sizeof(str) - 1;
    while (ii > 0 && str[ii - 1] == '\0') {
        str[ii - 1] = ' ';
        flength = ii--;    /* optional: keep track of our unpadded length */
    }
}

/* Once you've space padded the string you can call your Fortran method.
 * If your Fortran method accepts a string of unknown length, supply
 * `flength`. If your Fortran method expects a string of a fixed length, pass
 * the size of `str` (excluding '\0') instead.
 */
FCHARS(str, flength);

只要遵循Fortran编译器的ABI要求(例如CDECL,隐藏的字符串长度交错)从C / C ++代码,你会很好。

As long as you follow the ABI requirements of your Fortran compiler (e.g. CDECL, hidden string lengths passed interleaved) from the C/C++ code, you'll be fine.

这篇关于是否可以在c / c ++中的double(8字节)类型的变量中存储8个字符(每个1字节)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆