hi there i'm new here i hope you can help me through this homework
i've read like 14 pages of topics about seekg and fstream.. and well.. they didn't solve my problem.
my homework it's about creating a text file from "b to z" without the vocals, then reopen the file for reading/writing with fstream and print on screen the characters in pair position, and also print on screen the entire file in reverse (from z to b) reading with seekg.
i did the code in turbo c++ 3.0 (i know it's quite old but my teacher said we use that one because visual c and the new borland are for lazy people LOL). well i know where's the error maybe is my logic... but i don't know.. the file compiles good.. no error.. but when i run the exe it prints nothing on screen just make several lines of nothing like bunch of '\n' then stops. and hit any key to close it.. pretty weird.
here's the code

#include <iostream.h>
#include <fstream.h>
main()
{
 char minu, par,rw;
 int cont=0;
 ofstream arch("minus.txt"); //create a new .txt
  for(minu='b';minu<='z';minu++)
  {
   if(minu=='e'||minu=='i'||minu=='o'||minu=='u')
   {
    minu++;
   }
   arch<<minu;cont++;
  }  //write form b to z without the vocals
  arch.close();
 fstream arch2("minus.txt",ios::in|ios::out); //reopen the file for writing/reading
  if (arch2.fail())
  {
   cerr<<"Error al abrir el archivo"<<endl;
  } //chek if there's no error opening the file
  else
  {
   for(int t=cont;t>=0;t--)
   {
    arch2.seekg(t,ios::end);
    if(t%2==0)
    {
     par=arch2.get();
     cout<<par;
    }
   }
// read the file backwards from the last value of cont (must be 21) to 0, cheks if 't' position is pair, if it is must get the character in the pointer position and print it on screen.
   for(int t=cont;t>=0;t--)
   {
    arch2.seekg(t,ios::end);
    rw=arch2.get();
    cout.put(rw);
    }
// read the file backwards again and print each character.
 arch2.close();
 }
 cin.get();
 return 0;
 }

Recommended Answers

All 3 Replies

Three things.
1 - your use of get( ) is reading your data file as integers, not char. You need to use the form of get( ) with the destination variable as a parameter, like arch2.get( par); 2 - your positioning of the file pointer is off in nowhere land. Setting position arch2.seekg(t,ios::end); says go to the end of file, then move t positions further. You should either go to the ios::beg position with a 0 offset, or to the ending position with an offset of -t, to back you up to the beginning.

3 - in the second display loop, you need to keep in mind that the offset is 0-based counting, so the value of t is one past the last file item. Reading there puts your input in a bad state, and you'll get nothing usable in that loop. Try arch2.seekg(t-1,ios::beg);

i did the code in turbo c++ 3.0 (i know it's quite old but my teacher said we use that one because visual c and the new borland are for lazy people LOL)

I think your teacher is the lazy one - doesn't want to learn, or to teach you, how current, modern C++ is done. He/She is not properly preparing you for the real world of work or further computer science education.

I think your teacher is the lazy one - doesn't want to learn, or to teach you, how current, modern C++ is done. He/She is not properly preparing you for the real world of work or further computer science education.

LMAO.. well.. maybe that's true.. he's an old person...

thanx for the answer it solved my question.. the code ended up like this

#include <iostream.h>
#include <fstream.h>
main()
{
 char minu, par,rw;
 int cont=0;
 ofstream arch("minus.txt");
  for(minu='b';minu<='z';minu++)
  {
   if(minu=='e'||minu=='i'||minu=='o'||minu=='u')
   {
    minu++;
   }
   arch<<minu;cont++;
  }
  arch.close();
 fstream arch2("minus.txt",ios::in|ios::out);
  if (arch2.fail())
  {
   cerr<<"Error al abrir el archivo"<<endl;
  }
  else
  {
   for(int t=cont;t>=0;t--)
   {
    arch2.seekg(-t,ios::end);
    if(t%2==0)
    {
     arch2.get(par);
     cout<<par;
    }

   }
   cout<<'\n';
   for(int l=1;l<cont;l++)
   {
   arch2.clear();
    arch2.seekg(-l,ios::end);
    arch2.get(rw);
    cout<<rw;
   }
 arch2.close();
 cin.get();
  }
 return 0;
 }

Just a note - using 'l' (lower case 'L') as a variable name all by itself is looking for confusion - in some type faces it is virtually indistinguishable from the digit 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.