Can't getline from a file?

I could really use some assistance. I tried talking to a tutor, a TA and e-mailing the teacher(hasn't gotten back yet). I'm working on this for a class and I can't make the function for search work. I'm trying to get a line from a file I made and then check it for a keyword entered by the user. I don't know why I'm having such difficulty with this and nobody has been able to figure why it won't work. The search function is commented out because I tried it a whole bunch of different ways (getline, .get, put the file in a variable and print it, getch).

I'm working in windows but I've already tried it in a Linux system and it's been debugged. I'm really not sure what to try from here... Even theories could be helpful, though it doesn't seem to be having the if/ofstream open for the same file. It doesn't seem to be windows..
I'm running out of options.


Also, my teacher only allows us to use c-strings which seems to complicate things.


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cctype>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <string>


//function prototypes
void lists();
void print();
void search();

using namespace std;

//array of structs to hold events
struct events{
	
	char name[100],
	location[100], 
	description[100], 
	date[100],
	attendance[100], 
	enthusiasm[100];
};

events activity[50];
//identify and use the file to input and retrieve data.
ofstream indata;
ifstream input;

int ans;
char res;


int main(){
//keyword for to search for the right event, search to retrieve the line, position to find the keyword.
char keyword[10];
char search[100];
int position;

indata.open("summer-list.txt");
input.open("summer-list.txt");


do{
	
lists();

cout << "Would you like to search for an event with a keyword(2) or print(3)" << endl;
cin >> ans;
cin.ignore(100, '\n');
}while(res == 'y');


	if(ans ==2){
//	search();
}
		
	if (ans == 3){
	print();
}


indata.close();
return 0;
}

//Search for a word in the file
void search(){
	
//cout << "Enter a keyword: " << endl;
//	cin >> keyword;
	cin.ignore(100, '\n');
	
int length = strlen(activity[50].name);
//for(int m=0; m< length; m++){
//	while(strcmp(keyword, activity[m].name )==0)
//while(!input.eof()){
// input.get(search, 100, '\n');
 // input.get(search, 100, input.eof());
 //if(position = search.find(keyword ) != string::npos){
//	cout << search;

//char c;
  //while (is.get(c))          
   // std::cout << c;

//}
//}	
}

//print all events for the user
void print(){
	cout<< "Name   #   Location   #   Date   #   Attendance   #   Enthusiasm  " << endl;
	
	for(int s =0; s< 5; s++){
	cout << activity[s].name << "\t # " << activity[s].location << 
     "\t # " << activity[s].date << "\t # " << activity[s].attendance << "\t # " << activity[s].enthusiasm << endl;	
}
}

//add events to the list.
void lists(){
for(int s =0; s < 5; s++){
cout << "Enter the event name: ";
cin >> activity[s].name;
cin.ignore(100, '\n');
indata << activity[s].name << " # ";

cout << "Enter the event loaction: ";
cin>> activity[s].location;
cin.ignore(100, '\n');
indata << activity[s].location << " # ";


cout << "Enter the event date: ";
cin>> activity[s].date;
cin.ignore(100, '\n');
indata << activity[s].date << " # ";

cout << "Enter the event attendance: ";
cin>> activity[s].attendance;
cin.ignore(100, '\n');
indata << activity[s].attendance << " # ";

cout << "Enter the event enthusiasm: ";
cin>> activity[s].enthusiasm;
cin.ignore(100, '\n');
indata << activity[s].enthusiasm << " # " << '\n';

cout << "Would you like to continue to enter events(y/n): ";
cin >> res;
cin.ignore(100, '\n');

if (res != 'y' && res != 'Y'){
break;
}

	
}
	
	
}

Last edited on
You have no check to see if your file is open and I suspect it's not.
You are also trying to open the same file for read and write.
I'm pretty sure that's not allowed. You can open it for reading close it then open it for writing but not both at the same time.

This will show you one way to use ifstream.

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

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

using namespace std;

int main(){
std::ifstream input ("summer-list.txt");
string line;
// if file exist
if (input.good())
{
while(input.good())
	{
	getline (input,line);
	c
out << line << endl;
	}
}
// if file not found
else
{cout << "file not found";}


input.close();
return 0;
}

Last edited on
Thanks for the response! The assistant I was working with tested it by // out the other stream so it wouldn't conflict and it still wouldn't pull out a line from an ifstream. You're example worked fine by itself. I must've added something weird unintentionally, I'm gonna try moving the open/close streams.
Last edited on
Topic archived. No new replies allowed.