-1

I wrote the following code in codeblocks and since I am new to programming I would like to know the problem in simple words. Does the open() constructor create a new file if it does not exists?

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
  char str[80];
  cout<<"Enter a string : ";cin>>str;
  int len=strlen(str);
  fstream file;
  file.open("TEXT",ios::in|ios::out);
  for(int i=0;i<len;i++)
  file.put(str[i]);
  file.seekg(0);
  char ch;
  cout<<"\nPrintitng Contents....\n";
  int k=0;
  while(file)
  {
    file.seekg(k);
    file.get(ch);
    cout<<ch;
    k++;
  }

  return 0;
}
  • What makes you believe there is a problem? What outcome do you expect, what do you observe, and how do the two differ? – Igor Tandetnik Nov 18 '17 at 04:58
  • 3
    Seeing as you never check that status of the stream how sure are you that the file even opened? – user4581301 Nov 18 '17 at 05:03
  • Whenever possible, please, *please* use `std::string` and not some random length C-style character array. – tadman Nov 18 '17 at 05:08
  • put spaces in your code to make it readable. Besides, [`open` is not a constructor, and you should read its document first](http://en.cppreference.com/w/cpp/io/basic_fstream/open) – phuclv Nov 18 '17 at 05:12
  • Took me a while to find this chart (https://stackoverflow.com/a/26900463/4581301) outside the standard. By itself it's not that useful as an answer, but if you combine it with the chart here (http://en.cppreference.com/w/cpp/io/c/fopen), you'll see what's gone wrong. – user4581301 Nov 18 '17 at 05:23

2 Answers2

0

I think you don't have "TEXT". and fstream::open don't make file when if file that you want to read do not exist.

so you may try in different stream to read and write.

following code will help you.

#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

int main()
{
    char str[80];
    cout << "Enter a string : "; 
    cin >> str;
    int len = strlen(str);

    ofstream fout;
    fout.open("TEXT.txt");

    for (int i = 0; i<len; i++)
        fout.put(str[i]);

    fout.close();

    ifstream fin;
    fin.open("TEXT.txt");

    char ch;
    cout << "\nPrintitng Contents....\n";

    while (!fin.eof())
    {
        fin.get(ch);
        cout << ch;
        ch = NULL;
    }

    fin.close();

    return 0;
}

and you may improve your code like this

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string str;

    cout << "Enter a string : "; 
    cin >> str;

    ofstream fout;
    fout.open("TEXT.txt");

    fout << str;

    fout.close();

    str.clear();

    ifstream fin;
    fin.open("TEXT.txt");

    cout << "\nPrintitng Contents....\n";

    fin >> str;
    cout << str;

    fin.close();

    return 0;
}
이동근
  • 73
  • 7
  • Almost exactly right, but you need to give [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) a read and make a quick correction. – user4581301 Nov 18 '17 at 05:18
  • iostream::eof return true when the cursur reaches end of file, and return false in other case. so make loop keep looping while "not" end of file. – 이동근 Nov 18 '17 at 05:29
  • Second one is totally the right way to do it in C++, but the big problem with `while (!fin.eof())` is it tests for eof before reading and finding the eof. `fin.seekg(k);` or `fin.get(ch);` could fail for any reason including finding the eof and `ch` is still used, potentially printing garbage. In addition, if the read fails before finding the end of the file, you have an infinite loop because you'll never reach the end of the file. – user4581301 Nov 18 '17 at 05:36
  • 1
    I thought questioner's problem is just "can't open file". so i didn't think about read loop. in that case seekg() don't need to. fstream::get() increase cursur pointer automatically, so you can delete seekg(). but still the first loop print same character twice at last. it's because there is '\0' character in end of string. so you should clear ch in every loop. thank you for fix my fault – 이동근 Nov 18 '17 at 05:54
-1

Following code is more appropriate for C++, I think

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    string str;
    string newStr;
    cout << "Enter a string : "; cin >> str;
    int len = str.length();
    fstream file;
    file.open("TEXT", ios::out| ios::in );
    if (!file.is_open())
        return 0;
    file << str;
    file.seekg(0,file.beg);
    char ch;
    cout << "\nPrintitng Contents....\n";

    file >> newStr;
    cout << newStr;
    file.close();

    return 0;
}
ugur
  • 330
  • 4
  • 18