Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,379 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,570 people online right now. Registration is fast and FREE... Join Now!




File Processing: fstream

 
Reply to this topicStart new topic

File Processing: fstream, outputting

janandrada
23 Jan, 2008 - 07:29 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
This is a program that translates phone key letter(from a file) to a number. I've outputted the converted letters in screen. Now, how can I write the converted letters to another file?

here's my code:
CODE

#include <iostream>
#include <fstream>
using namespace std;
void num(char ch){
  if(ch == '\n')
        cout << endl;
  if(ch == 'a' || ch == 'A' || ch == 'b' || ch == 'B' || ch == 'c' || ch == 'C')
        cout << "2";
  if(ch == 'd' || ch == 'D' || ch == 'e' || ch == 'E' || ch == 'f' || ch == 'F')
        cout << "3";
  if(ch == 'g' || ch == 'G' || ch == 'h' || ch == 'H' || ch == 'i' || ch == 'I')
        cout << "4";
  if(ch == 'j' || ch == 'J' || ch == 'k' || ch == 'K' || ch == 'l' || ch == 'L')
        cout << "5";
  if(ch == 'm' || ch == 'M' || ch == 'n' || ch == 'N' || ch == 'o' || ch == 'O')
        cout << "6";
  if(ch == 'p' || ch == 'P' || ch == 'q' || ch == 'Q' || ch == 'r' || ch == 'R' || ch == 's' || ch == 'S')
        cout << "7";
  if(ch == 't' || ch == 'T' || ch == 'u' || ch == 'U' || ch == 'v' || ch == 'V')
        cout << "8";
  if(ch == 'w' || ch == 'W' || ch == 'x' || ch == 'X' || ch == 'y' || ch == 'Y' || ch == 'z' || ch == 'Z')
        cout << "9";  
}
int main (){
    ifstream in;
    char ch;
    
    in.open("numbers.txt");
    
    while(!in.eof()){
             ch = in.get();
             num(ch);
             }
    in.close();
    
    ofstream out;
    out.open("numbers2.txt");
             out << ch;
    out.close();                
    
return 0;  
}


User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: File Processing: Fstream
23 Jan, 2008 - 08:02 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Well, first off you need to return a value from the function. That value can be stored in a data structure, or placed directly into the out file. How about placing both the in and the out in the same "while" loop like this:
CODE


int num(char ch) {
  int returnValue;
  if(ch == '\n')
        cout << endl;
  if(ch == 'a' || ch == 'A' || ch == 'b' || ch == 'B' || ch == 'c' || ch == 'C') {
        cout << "2"; returnValue = '2';
  }
  if ...

  ..
  return returnValue;
}

...
int main (){
  ifstream in;
  ofstream out;

  in.open("numbers.txt");
  out.open("numbers2.txt");


  while(!in.eof()){
             ch = in.get();
             out << num(ch);
  }
  
  in.close();
  out.close();

  return 0;
}

This avoids saving the values in a data structure, but it also avoids formating the data in a useful way. A "case" statement would work better than all those "if" statements, also.

wink2.gif

This post has been edited by ajwsurfer: 23 Jan, 2008 - 08:02 AM
User is offlineProfile CardPM
+Quote Post

janandrada
RE: File Processing: Fstream
23 Jan, 2008 - 05:59 PM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
Its still doesnt work. I can't find what's wrong.

CODE

using namespace std;
int num(char ch){
int returnValue;  
  if(ch == '\n')
        cout << endl;
  if(ch == 'a' || ch == 'A' || ch == 'b' || ch == 'B' || ch == 'c' || ch == 'C')
        cout << "2"; returnValue = '2';
  if(ch == 'd' || ch == 'D' || ch == 'e' || ch == 'E' || ch == 'f' || ch == 'F')
        cout << "3"; returnValue = '3';
  if(ch == 'g' || ch == 'G' || ch == 'h' || ch == 'H' || ch == 'i' || ch == 'I')
        cout << "4"; returnValue = '4';
  if(ch == 'j' || ch == 'J' || ch == 'k' || ch == 'K' || ch == 'l' || ch == 'L')
        cout << "5"; returnValue = '5';
  if(ch == 'm' || ch == 'M' || ch == 'n' || ch == 'N' || ch == 'o' || ch == 'O')
        cout << "6"; returnValue = '6';
  if(ch == 'p' || ch == 'P' || ch == 'q' || ch == 'Q' || ch == 'r' || ch == 'R' || ch == 's' || ch == 'S')
        cout << "7"; returnValue = '7';
  if(ch == 't' || ch == 'T' || ch == 'u' || ch == 'U' || ch == 'v' || ch == 'V')
        cout << "8"; returnValue = '8';
  if(ch == 'w' || ch == 'W' || ch == 'x' || ch == 'X' || ch == 'y' || ch == 'Y' || ch == 'z' || ch == 'Z')
        cout << "9"; returnValue = '9';
  return returnValue;        
}
int main (){
    char ch;
    ifstream in;
    ofstream out;
    
    in.open("numbers.txt");
    out.open("numbers2.txt");
    
    while(!in.eof()){
             ch = in.get();
             out << num(ch);
             }

    in.close();        
    out.close();                
    
return 0;  
}


User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: File Processing: Fstream
24 Jan, 2008 - 09:45 AM
Post #4

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
This worked for me in MS Visual C++ .Net
Not glamorous and probably not even accurate, but it worked:
CODE

// cppFirst.cpp : main project file.

#include "stdafx.h"

#include <iostream>
#include <fstream>
using namespace std;

int num(char ch){
int returnValue;  
  if(ch == '\n')
        cout << endl;
  if(ch == 'a' || ch == 'A' || ch == 'b' || ch == 'B' || ch == 'c' || ch == 'C')
        cout << "2"; returnValue = '2';
  if(ch == 'd' || ch == 'D' || ch == 'e' || ch == 'E' || ch == 'f' || ch == 'F')
        cout << "3"; returnValue = '3';
  if(ch == 'g' || ch == 'G' || ch == 'h' || ch == 'H' || ch == 'i' || ch == 'I')
        cout << "4"; returnValue = '4';
  if(ch == 'j' || ch == 'J' || ch == 'k' || ch == 'K' || ch == 'l' || ch == 'L')
        cout << "5"; returnValue = '5';
  if(ch == 'm' || ch == 'M' || ch == 'n' || ch == 'N' || ch == 'o' || ch == 'O')
        cout << "6"; returnValue = '6';
  if(ch == 'p' || ch == 'P' || ch == 'q' || ch == 'Q' || ch == 'r' || ch == 'R' || ch == 's' || ch == 'S')
        cout << "7"; returnValue = '7';
  if(ch == 't' || ch == 'T' || ch == 'u' || ch == 'U' || ch == 'v' || ch == 'V')
        cout << "8"; returnValue = '8';
  if(ch == 'w' || ch == 'W' || ch == 'x' || ch == 'X' || ch == 'y' || ch == 'Y' || ch == 'z' || ch == 'Z')
        cout << "9"; returnValue = '9';
  return returnValue;        
}


int main(array<System::String ^> ^args)
{


    char ch;
    ifstream in;
    ofstream out;

    in.open("numbers.txt");
    out.open("numbers2.txt");

    while(!in.eof()){
      ch = in.get();
      out << num(ch);
    }

    in.close();        
    out.close();  

    return 0;
}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:22AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month