Streams in C++
It´s in C++ and it dosen´t work when i run it, it dosen´t crash or something, but i don´t get the output that I want.
Show us the code! And tell us what you expect it to do.
--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.
--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.
quote:Original post by SabreMan
I''ve not seen any mention in this thread of which language you are using. Since this forum is about "General Programming", it could be any language. Most people seem to have assumed you are using C++, but is that the case?
The title "Streams in C++" pretty much gave it away for me
Here is the code:
#include <fstream.h>
void main
{
ifstream fin("file.txt");
char string;
fin.getline(&string,sizeof(string));
cout << string;
}
I except it to print the whole first line in file.txt.
#include <fstream.h>
void main
{
ifstream fin("file.txt");
char string;
fin.getline(&string,sizeof(string));
cout << string;
}
I except it to print the whole first line in file.txt.
Several problems:
- you should not be using the old-style headers.
- don't use char array, use std::string.
- use the global getline() function to get a line into a string.
Here's a modified version of the code:
Oh, and the signature of main() must always return an int. Returning void is illegal. If you don't explicitly return an int, the compiler inserts a "return 0" into your code.
--
The placement of a donkey's eyes in its head enables it to see all four feet at all times.
Edited by - SabreMan on February 24, 2002 10:58:26 AM
- you should not be using the old-style headers.
- don't use char array, use std::string.
- use the global getline() function to get a line into a string.
Here's a modified version of the code:
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ ifstream fin("file.txt"); string s; getline(fin, s); cout << s;}
Oh, and the signature of main() must always return an int. Returning void is illegal. If you don't explicitly return an int, the compiler inserts a "return 0" into your code.
--
The placement of a donkey's eyes in its head enables it to see all four feet at all times.
Edited by - SabreMan on February 24, 2002 10:58:26 AM
#include <stdio.h>#define LENGTH 128FILE f;char string[LENGTH];if(f = fopen("file.txt", "r") == NULL) return;else{fgets(string, LENGTH, f);fclose(f);}
btw: char *fgets(char *s, int n, FILE *stream);
hope this helps
If you''re using C++, please use C++. SabreMan''s preceding example is the definitve C++ solution (a modification of DrPizza''s to handle fstreams). std::getline handles all istream-derived objects (make sure to use the new, sans-''.h'' extension standard library header files).
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
quote:Original post by SabreMan
Oh, and the signature of main() must always return an int. Returning void is illegal. If you don''t explicitly return an int, the compiler inserts a "return 0" into your code.
actually, its OK if you dont return a number in main()
quote:Original post by barazor
actually, its OK if you dont return a number in main()
As long as you declare it void (I think ANSI standard says that you''re supposed to return a value, but every compiler I''ve ever seen lets you declare main() as a void)
"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
quote:Original post by matrix2113
As long as you declare it void (I think ANSI standard says that you''re supposed to return a value, but every compiler I''ve ever seen lets you declare main() as a void )
Rather than second guessing what the Standard says, why don''t you read what it says? It states that not returning a value from main() is an implicit return of zero. It also states that any signature of main involving anything other than an int return type is illegal. MS may get that wrong, but they don''t define the Standard.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement