The Concept of fstream etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iostream>
using namespace std;

int main() 
{
  char buffer[256];
  cout << "Please enter path to the file: " << endl;
  fstream myfile ("C:/test.txt");

  while (!myfile.eof() )
  {
	myfile.getline (buffer,100);
	cout << buffer << "1" << endl;
        cout << buffer << "12" << endl;
        cout << buffer << "123" << endl;
  }
  return 0;
}


Basically, I would like to know two things, or how to go about doing the following, some tips, clues, or sample code to work from :)

1). How to allow a user to actually type in a file location, therefore allowing the program to ask cout << "please enter file location" << endl;... and then enter that into a variable, and use that variable as the file for the program to open and edit.

2). Like I have added 1 to each word, then 12, then 123. What if I wanted to change the first letter of each word to a capital letter?

Regards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char buffer[256]; //This will fail in cases where there are more than 256 bytes per line
    cout << "Please enter path to the file: " << endl;
    string path;
    cin >> path;
    fstream myfile(path.c_str());
    while(!myfile.eof() )
    {
        myfile.getline(buffer, 100); //the way you have it now cout << buffer << "1" will add 1 to //each line, not to each word,
        cout << buffer << "1" << endl; 
        cout << buffer << "12" << endl; //this will repeat the same line, only adding a 12 instead of 
//a 1        
    }

}


what you probably want instead is:
1
2
3
4
5
6
7
8
9
while(!myfile.eof())
{
    myfile >> buffer; //not sure this works with char arrays, you may want to change char buffer //to string buffer
    buffer[0] = toupper(buffer[0]); //I forget which library offhand you have to include to use this //function
    cout << buffer << "1" << endl;
    myfile >> buffer; //you may want to clear the buffer first
    buffer[0] = toupper(buffer[0]);
    cout << buffer << "12" << endl;
}
Thanks for the help :)

How would I specify to just add to the words (not lines), I assume change myfile.getline (modify the .getline part of the function).

Thus from this, work out how to edit specific characters of each word, any ideas?
myfile >> buffer; is what you want. It reads from the file until the first space is encountered. So the size of buffer will be however big each word is. (Assuming you declare buffer as string buffer).
Then you can modify each character in buffer with the index operator []. You can get the size of buffer with buffer.size() etc, and use a for loop to modify each character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <fstream>
#include <iostream>
using namespace std;
int UserChoice(int x);

int main() 
{    
  int a; 
  UserChoice(a);
  string buffer;
  cout << "Please enter path to the file: " << endl;
  string fDirectory;
  cin >> fDirectory;
  fstream myfile (fDirectory.c_str());

  while (!myfile.eof())
  {
    myfile >> buffer; 
    buffer[0] = toupper(buffer[0]);
    if(a=1) {
    cout << buffer << "1" << endl;  
    }
    else if(a=2){
    cout << buffer << "12" << endl;    
    }
    else if(a=3){
    cout << buffer << "123" << endl;    
    }      
  }
  cout << "Press ENTER to continue..." << endl;
  cin.get();cin.ignore();
  return 0;
}

int UserChoice(int x) {
    cout << "Welcome to the Word List Generator Verson 1.0" << endl;
    cout << "Please choose from the following options: " << endl;
    cout << "1. Append 1 to the end of each and every word and Capitalize first char" << endl;
    cout << "2. Append 12 to the end of each and every word and Capitalize first char" << endl;
    cout << "3. Append 123 to the end of each and every word and Capitalize first char." << endl;
    cin >> x;
    cout << "You chose option: " << x << endl;   
    return x;
}


Hey again, created a function which allows the user to choose an option (from 3), then returns it, I call the function in main, and insert an IF into my while loop, but the outcome is the same no matter what number I enter, whether it be 1, 2 or 3, it always, only appends 1 onto the end of each word. Any ideas what is wrong with my code?
First, change int UserChocie(int x) to int UserChoice()
Second, change the call to a = UserChoice();
Third, place the call to within the while loop.
1 other thing, change if (a=1), if (a=2), if (a=3) to if (a==1), if (a ==2), if (a==3) respectively. what you are doing now is assigned a to 1, a to 2, and a to 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* 
 * File:   main.cpp
 * Author: Damien
 *
 * Created on 05 September 2011, 23:46
 */

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
void centertext(char* s);
int UserChoice();
int main() 
{    
  int a; 
  a = UserChoice();
  if(a==4){
      centertext("Thankyou for using Wordlist Generator 1.1!");
  exit (1);
  }     
  string buffer;
  cout << "Please enter path to the file: " << endl;
  string fDirectory;
  cin >> fDirectory;
  fstream myfile (fDirectory.c_str());

  while (!myfile.eof())
  {
    myfile >> buffer; 
    buffer[0] = toupper(buffer[0]);
    if(a==1) {
    cout << buffer << "1" << endl;  
    }
    else if(a==2){
    cout << buffer << "12" << endl;    
    }
    else if(a==3){
    cout << buffer << "123" << endl; 
    }
  }
  return 0;
}

int UserChoice() {
    int x;
    cout << "Welcome to the Word List Generator Version 1.0" << endl;
    cout << "Please choose from the following options: " << endl;
    cout << "1. Append 1 to the end of each and every word and Capitalize first char" << endl;
    cout << "2. Append 12 to the end of each and every word and Capitalize first char" << endl;
    cout << "3. Append 123 to the end of each and every word and Capitalize first char." << endl;
    cout << "4. Exit" << endl;
    cin >> x;
    cout << endl;
    cout << "You chose option: " << x << endl;   
    cout << endl;
    return x;
}

void centertext(char* s){
    int l = strlen(s);
    int pos = (int)((80-l)/2);
    for(int i=0; i<pos; i++) 
        cout << " ";
    cout << s << endl;   
}




So I got the basics working, now I would like to move on, now I have modified the words, how can I write them to a new file or the existing file (or both), I assume using ofstream? and also allow the user to choose a name for the new .txt file being created.
Bump - any advice :) ?
Topic archived. No new replies allowed.