Forum: PC-Programmierung c++ Newline-Größe für Txt-Dateien auf aktueller Platform ermitteln?


von X. H. (shadow0815)


Lesenswert?

Hi!
Ich habe einen Dateizeiger und muss aber auf die Postion 
Dateizeiger-size_of_newline springen.

Je nach Plattform ist das Newline 1 oder 2 chars lang.
Wie kann ich "size_of_newline_in_file_for_this_platform" ermitteln?

Oder geht nur der (meiner Meinung nach nicht elegante) Weg:

-Neue leere Datei im Textmode erstellen
-Newline reinschreiben
-größe ermitteln
-datei löschen
?

Folgende Fakten kenne ich:
http://en.wikipedia.org/wiki/Newline

helfen mir aber nicht unter c++ "elegant und platformunabhängig" 
"size_of_newline_in_file_for_this_platform" zu ermitteln.

Oder gibt es standardmäßige defines, mit denen ich fragen kann, ob ich 
gerade unter windows oder was anderem compiliere?

von der mechatroniker (Gast)


Lesenswert?

Ein '\n' ist immer 1 Zeichen, auf jeder Plattform. Es gibt lediglich 
"weiche" Konventionen, ob man "\r", "\n" oder "\r\n" benutzt. Es zwingt 
keiner einen Dateiersteller, sich daran zu halten. In Zeiten der 
Netzwerke kann man sich sowieso nicht drauf verlassen, auf welcher 
Plattform die Datei erstellt wurde.

Lösung: In der konkreten Datei aufs vorletzte Zeichen springen, prüfen 
ob dies '\r' ist, wenn ja noch ein Zeichen zurückspringen.

von Matthias L. (matze88)


Lesenswert?

Wenn du vor's NewLine springen willst, kannst du doch einfach 1 zurück 
springen und schauen, obs ein \r ist oder nicht. Falls ja, noch einen 
zurück.

Matthias

von Klaus W. (mfgkw)


Lesenswert?

geht es nur um den gcc?
Der definiert unter Unix/Linux etc. das Makro _unix_.

Ich vermute, daß er unter Windows etwas ähnliches definiert.

Visual C++ definiert dort wiederum _WIN32.
Ob der gcc unter Windows das auch macht, müsste man probieren.

Die jeweils definierten Makros bekommt man beim gcc mit
dem Kommando:
1
cpp -dM
Der Befehl wartet danach auf Eingabe aus stdin, das kann man gleich mit
Ctrl-D beenden und bekommt alle definierten Makros gelistet.
Unter Linux sieht das etwas so aus:
1
#define __DBL_MIN_EXP__ (-1021)
2
#define __FLT_MIN__ 1.17549435e-38F
3
#define __DEC64_DEN__ 0.000000000000001E-383DD
4
#define __CHAR_BIT__ 8
5
#define __WCHAR_MAX__ 2147483647
6
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
7
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
8
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
9
#define __DBL_DENORM_MIN__ 4.9406564584124654e-324
10
#define __FLT_EVAL_METHOD__ 2
11
#define __unix__ 1
12
#define __DBL_MIN_10_EXP__ (-307)
13
#define __FINITE_MATH_ONLY__ 0
14
#define __GNUC_PATCHLEVEL__ 2
15
#define __DEC64_MAX_EXP__ 384
16
#define __SHRT_MAX__ 32767
17
#define __LDBL_MAX__ 1.18973149535723176502e+4932L
18
#define __UINTMAX_TYPE__ long long unsigned int
19
#define __linux 1
20
#define __DEC32_EPSILON__ 1E-6DF
21
#define __unix 1
22
#define __LDBL_MAX_EXP__ 16384
23
#define __linux__ 1
24
#define __SCHAR_MAX__ 127
25
#define __DBL_DIG__ 15
26
#define __SIZEOF_INT__ 4
27
#define __SIZEOF_POINTER__ 4
28
#define __USER_LABEL_PREFIX__ 
29
#define __STDC_HOSTED__ 1
30
#define __LDBL_HAS_INFINITY__ 1
31
#define __FLT_EPSILON__ 1.19209290e-7F
32
#define __LDBL_MIN__ 3.36210314311209350626e-4932L
33
#define __DEC32_MAX__ 9.999999E96DF
34
#define __SIZEOF_LONG__ 4
35
#define __DECIMAL_DIG__ 21
36
#define __gnu_linux__ 1
37
#define __LDBL_HAS_QUIET_NAN__ 1
38
#define __GNUC__ 4
39
#define __FLT_HAS_DENORM__ 1
40
#define __SIZEOF_LONG_DOUBLE__ 12
41
#define __DBL_MAX__ 1.7976931348623157e+308
42
#define __DBL_HAS_INFINITY__ 1
43
#define __DEC32_MIN_EXP__ (-95)
44
#define __LDBL_HAS_DENORM__ 1
45
#define __DEC128_MAX__ 9.999999999999999999999999999999999E6144DL
46
#define __DEC32_MIN__ 1E-95DF
47
#define __DBL_MAX_EXP__ 1024
48
#define __DEC128_EPSILON__ 1E-33DL
49
#define __LONG_LONG_MAX__ 9223372036854775807LL
50
#define __SIZEOF_SIZE_T__ 4
51
#define __SIZEOF_WINT_T__ 4
52
#define __GXX_ABI_VERSION 1002
53
#define __FLT_MIN_EXP__ (-125)
54
#define __DBL_MIN__ 2.2250738585072014e-308
55
#define __DECIMAL_BID_FORMAT__ 1
56
#define __DEC128_MIN__ 1E-6143DL
57
#define __REGISTER_PREFIX__ 
58
#define __DBL_HAS_DENORM__ 1
59
#define __NO_INLINE__ 1
60
#define __i386 1
61
#define __FLT_MANT_DIG__ 24
62
#define __VERSION__ "4.3.2"
63
#define __DEC64_EPSILON__ 1E-15DD
64
#define __DEC128_MIN_EXP__ (-6143)
65
#define __i486__ 1
66
#define unix 1
67
#define __i386__ 1
68
#define __SIZE_TYPE__ unsigned int
69
#define __DEC32_DEN__ 0.000001E-95DF
70
#define __ELF__ 1
71
#define __FLT_RADIX__ 2
72
#define __LDBL_EPSILON__ 1.08420217248550443401e-19L
73
#define __SIZEOF_PTRDIFF_T__ 4
74
#define __FLT_HAS_QUIET_NAN__ 1
75
#define __FLT_MAX_10_EXP__ 38
76
#define __LONG_MAX__ 2147483647L
77
#define __FLT_HAS_INFINITY__ 1
78
#define __DEC64_MAX__ 9.999999999999999E384DD
79
#define __DEC64_MANT_DIG__ 16
80
#define __DEC32_MAX_EXP__ 96
81
#define linux 1
82
#define __DEC128_DEN__ 0.000000000000000000000000000000001E-6143DL
83
#define __LDBL_MANT_DIG__ 64
84
#define __DBL_HAS_QUIET_NAN__ 1
85
#define __WCHAR_TYPE__ int
86
#define __SIZEOF_FLOAT__ 4
87
#define __DEC64_MIN_EXP__ (-383)
88
#define __FLT_DIG__ 6
89
#define __INT_MAX__ 2147483647
90
#define __i486 1
91
#define __FLT_MAX_EXP__ 128
92
#define __DBL_MANT_DIG__ 53
93
#define __DEC64_MIN__ 1E-383DD
94
#define __WINT_TYPE__ unsigned int
95
#define __SIZEOF_SHORT__ 2
96
#define __LDBL_MIN_EXP__ (-16381)
97
#define __LDBL_MAX_10_EXP__ 4932
98
#define __DBL_EPSILON__ 2.2204460492503131e-16
99
#define __SIZEOF_WCHAR_T__ 4
100
#define __DEC_EVAL_METHOD__ 2
101
#define __INTMAX_MAX__ 9223372036854775807LL
102
#define __FLT_DENORM_MIN__ 1.40129846e-45F
103
#define __FLT_MAX__ 3.40282347e+38F
104
#define __SIZEOF_DOUBLE__ 8
105
#define __FLT_MIN_10_EXP__ (-37)
106
#define __INTMAX_TYPE__ long long int
107
#define i386 1
108
#define __DEC128_MAX_EXP__ 6144
109
#define __GNUC_MINOR__ 3
110
#define __DEC32_MANT_DIG__ 7
111
#define __DBL_MAX_10_EXP__ 308
112
#define __LDBL_DENORM_MIN__ 3.64519953188247460253e-4951L
113
#define __STDC__ 1
114
#define __PTRDIFF_TYPE__ int
115
#define __DEC128_MANT_DIG__ 34
116
#define __LDBL_MIN_10_EXP__ (-4931)
117
#define __SIZEOF_LONG_LONG__ 8
118
#define __LDBL_DIG__ 18
119
#define __GNUC_GNU_INLINE__ 1

FÜr Windows hatte ich mir mal notiert:
1
Microsoft VC++ (aus MSDN April 2000)
2
3
Predefined Macros
4
The compiler recognizes six predefined ANSI C macros (see Table 1.1),
5
and the Microsoft C++ implementation provides several more (see Table
6
1.2). These macros take no arguments and cannot be redefined. Their
7
value (except for __LINE__ and __FILE__) must be constant throughout
8
compilation. Some of the predefined macros listed below are defined
9
with multiple values. Their values can be set by selecting the
10
corresponding menu option in the Visual C++ development environment,
11
or by using a command-line switch. See the tables below for more
12
information.
13
14
Table 1.1   ANSI Predefined Macros
15
16
Macro Description
17
__DATE__ The compilation date of the current source file. The date is
18
a string literal of the form Mmm dd yyyy. The month name Mmm is the
19
same as for dates generated by the library function asctime declared
20
in TIME.H.
21
__FILE__ The name of the current source file. __FILE__ expands to a
22
string surrounded by double quotation marks.
23
__LINE__ The line number in the current source file. The line number
24
is a decimal integer constant. It can be altered with a #line
25
directive.
26
__STDC__ Indicates full conformance with the ANSI C standard. Defined
27
as the integer constant 1 only if the /Za compiler option is given and
28
you are not compiling C++ code; otherwise is undefined.
29
__TIME__ The most recent compilation time of the current source
30
file. The time is a string literal of the form hh:mm:ss.
31
__TIMESTAMP__ The date and time of the last modification of the
32
current source file, expressed as a string literal in the form Ddd Mmm
33
Date hh:mm:ss yyyy, where Ddd is the abbreviated day of the week and
34
Date is an integer from 1 to 31.
35
36
37
38
39
Table 1.2   Microsoft-Specific Predefined Macros
40
41
Macro Description
42
_CHAR_UNSIGNED Default char type is unsigned. Defined when /J is specified.
43
__cplusplus Defined for C++ programs only.
44
_CPPRTTI Defined for code compiled with /GR (Enable Run-Time Type
45
Information).
46
_CPPUNWIND Defined for code compiled with /GX (Enable Exception
47
Handling).
48
_DLL Defined when /MD or /MDd (Multithread DLL) is specified.
49
_M_ALPHA Defined for DEC ALPHA platforms. It is defined as 1 by the
50
ALPHA compiler, and it is not defined if another compiler is used.
51
_M_IX86 Defined for x86 processors. See Table 1.3 for more details.
52
_M_MPPC Defined for Power Macintosh platforms. Default is 601
53
(/QP601). See Table 1.4 for more details.
54
_M_MRX000 Defined for MIPS platforms. Default is 4000 (/QMR4000). See
55
Table 1.5 for more details.
56
_M_PPC Defined for PowerPC platforms. Default is 604 (/QP604). See
57
Table 1.6 for more details.
58
_MFC_VER Defines the MFC version. Defined as 0x0421 for Microsoft
59
Foundation Class Library 4.21. Always defined.
60
_MSC_EXTENSIONS This macro is defined when compiling with the /Ze
61
compiler option (the default).  Its value, when defined, is 1.
62
_MSC_VER Defines the compiler version. Defined as 1200 for Microsoft
63
Visual C++ 6.0. Always defined.
64
_MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd
65
(Multithreaded) is specified.
66
_WIN32 Defined for applications for Win32®. Always defined.
67
68
69
As shown in following tables, the compiler generates a value for the
70
preprocessor identifiers that reflect the processor option specified.
71
72
Table 1.3   Values for _M_IX86
73
74
Option in Developer Studio Command-Line Option Resulting Value
75
Blend /GB _M_IX86 = 500 (Default. Future compilers will emit a
76
different value to reflect the dominant processor.)
77
Pentium /G5 _M_IX86 = 500
78
Pentium Pro /G6 _M_IX86 = 600
79
80386 /G3 _M_IX86 = 300
80
80486 /G4 _M_IX86 = 400
81
82
83
84
85
Table 1.4   Values for _M_MPPC
86
87
Option in development environment Command-Line Option Resulting Value
88
PowerPC 601 /QP601 _M_MPPC = 601 (Default)
89
PowerPC 603 /QP603 _M_MPPC = 603
90
PowerPC 604 /QP604 _M_MPPC = 604
91
PowerPC 620 /QP620 _M_MPPC = 620
92
93
94
95
96
Table 1.5   Values for _M_MRX000
97
98
Option in Developer Studio Command-Line Option Resulting Value
99
R4000 /QMR4000 _M_MRX000 = 4000 (Default)
100
R4100 /QMR4100 _M_MRX000 = 4100
101
R4200 /QMR4200 _M_MRX000 = 4200
102
R4400 /QMR4400 _M_MRX000 = 4400
103
R4600 /QMR4600 _M_MRX000 = 4600
104
R10000 /QMR10000 _M_MRX000 = 10000
105
106
107
108
109
Table 1.6   Values for _M_PPC
110
111
Option in Developer Studio Command-Line Option Resulting Value
112
PowerPC 601 /QP601 _M_PPC = 601
113
PowerPC 603 /QP603 _M_PPC = 603
114
PowerPC 604 /QP604 _M_PPC = 604 (Default)
115
PowerPC 620 /QP620 _M_PPC = 620
116
117
_MSC_VER 
118
 Reports the major and minor versions of the compiler. For example, 1310 for Microsoft Visual C++ .NET 2003. 1310 represents version 13 and a 1.0 point release. The Visual C++ 2005 compiler version is 1400.
119
120
 Type cl /? at the command line to see the major and minor versions of your compiler along with the build number.

Und gcc unter OSX:
1
Aus http://developer.apple.com/technotes/tn2002/tn2071.html:
2
3
Predefined Macros
4
5
As specified in Apple's developer GNU C Preprocessor
6
documentation[43], the following macros are predefined in Mac OS X:
7
8
__OBJC__
9
    This macro is defined when you compile Objective-C .m files or
10
    Objective-C++ .mm files, or when you override the file extension
11
    with -ObjC or -ObjC++ flags.
12
__ASSEMBLER__
13
    This macro is defined when compiling .s files.
14
__NATURAL_ALIGNMENT__
15
    This macro is defined on systems that use natural alignment. When
16
    using natural alignment, an int is aligned on sizeof(int)
17
    boundary, a short int is aligned on sizeof(short) boundary, and so
18
    on. It's defined by default when you're compiling code for the
19
    PowerPC, SPARC, and HPPA. It's not defined when you use the
20
    -malign-mac68k compiler switch.
21
__STRICT_BSD__
22
    This macro is defined if the -bsd switch was specified when GNU C
23
    was invoked.
24
__MACH__
25
    This macro is defined if Mach system calls are supported.
26
__APPLE__
27
    This macro is defined in any Apple computer.
28
__APPLE_CC__
29
    This macro is set to an integer that represents the version number
30
    of the compiler. This lets you distinguish, for example, between
31
    compilers based on the same version of GCC, but with different bug
32
    fixes or features. Larger values denote later compilers.
33
__BIG_ENDIAN__
34
    This macro sets the target architecture to be a most significant
35
    bit. See the Endian Issues sections for more details.
36
Note: To define a section of code to be compiled on Mac OS X system
37
    you should define a section using __APPLE__ with __MACH__
38
    macros. The macro __UNIX__ is not supported in Mac OS X.


Wer den gcc unter Windows hat, kann ja vielleicht mal dort
das cpp -dM machen und hier ergänzen, dann hat man die wichtigsten
an einer Stelle...

von X. H. (shadow0815)


Lesenswert?

Ich benutz jetzt den Binär-Modus. Der macht was ich will, wenn ich die 
getline selbst implementiere. Im Textmode wird sogar beim Abrufen des 
Lesezeigers dieser verstellt und das nächste line-get ging schief...

@ Klaus Wachtler (mfgkw)

Unter Windows hab ich MinGW
Wenn ich
cpp -dM eingebe und dann Ctrl+c kommt nix, bei Ctrl+d kommt ^D (ohne 
Abbruch im stdin-mode).

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.