473,503 Members | 6,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading File, Searching, Output

5 New Member
Hi all. I have a problem

I want my program to do the following.

Create a file of student ID #s.
Then, Read the file.
Search for the line of student ID # say 10002.
At that line, input test scores received from input earlier in program.

Initial file should look like:

10001
10002
10003
10004

After output:

10001
10002 65 45 35 34 //or "65,45,35,34" does not matter if there are spaces, commas, etc.
10003
10004

Now, I can create the student ID file fine, But I am not sure how to read and output exactly on that line. I am using fstream, but it seems to erase the whole file when it is finished.

Any help is appreciated. Thank you.
Apr 24 '07 #1
8 1533
Sharkolomew
5 New Member
Any ideas, please?
Apr 26 '07 #2
r035198x
13,262 MVP
Hi all. I have a problem

I want my program to do the following.

Create a file of student ID #s.
Then, Read the file.
Search for the line of student ID # say 10002.
At that line, input test scores received from input earlier in program.

Initial file should look like:

10001
10002
10003
10004

After output:

10001
10002 65 45 35 34 //or "65,45,35,34" does not matter if there are spaces, commas, etc.
10003
10004

Now, I can create the student ID file fine, But I am not sure how to read and output exactly on that line. I am using fstream, but it seems to erase the whole file when it is finished.

Any help is appreciated. Thank you.
I hate to ask for code but maybe if you post the code that you think is wrong ...
Apr 26 '07 #3
Sharkolomew
5 New Member
sure,

this code represents modifying the student data after the data file contains lines for each student number.

Expand|Select|Wrap|Line Numbers
  1. cout<<"Enter student ID # to modify: ";
  2. cin>>studentID;
  3. cout<<"Enter all student scores: ";
  4. cin>>scores;  //this could be either "50 50 50" or "50,50,50" if its easier to read in
  5. fstream f;
  6. f.open("data.txt");
  7. while(f>>input)
  8.    if (input==studentID)
  9.       {
  10.           cout<<"found";                 //shows that it is finding the student ID
  11.            f<<studentID<<" "<<scores<<end;    //i think the problem is here
  12.       }
  13. }
  14.  
Apr 26 '07 #4
ilikepython
844 Recognized Expert Contributor
sure,

this code represents modifying the student data after the data file contains lines for each student number.

Expand|Select|Wrap|Line Numbers
  1. cout<<"Enter student ID # to modify: ";
  2. cin>>studentID;
  3. cout<<"Enter all student scores: ";
  4. cin>>scores;  //this could be either "50 50 50" or "50,50,50" if its easier to read in
  5. fstream f;
  6. f.open("data.txt");
  7. while(f>>input)
  8.    if (input==studentID)
  9.       {
  10.           cout<<"found";                 //shows that it is finding the student ID
  11.            f<<studentID<<" "<<scores<<end;    //i think the problem is here
  12.       }
  13. }
  14.  
I think you need to open the file in append mode. That way you can add on to it without erasing the whole file. Try this:
Expand|Select|Wrap|Line Numbers
  1. f.open("data.txt", ios_base::app);
  2.  
but I'm not sure if that's the way to set it to append mode.
Also, you might want to try using the fseek function. That way you can go to a position in a file.
Apr 26 '07 #5
Savage
1,764 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. f.open("data.txt", ios_base::app);
  2.  
but I'm not sure if that's the way to set it to append mode.
.
Yes,that's the way for sure!!!

Nice ilikepython.

Savage
Apr 27 '07 #6
Sharkolomew
5 New Member
Okay, my code is no longer disappearing, but it is not putting out the scores still.

How does the fseek function work?
Apr 27 '07 #7
Savage
1,764 Recognized Expert Top Contributor
Okay, my code is no longer disappearing, but it is not putting out the scores still.

How does the fseek function work?
first here is declaration: int fseek(FILE *stream,offset,whence);
fseek function works this way:

It sets a pointer assigned to the FILE stream on location in file which depands on offset and whence.Offset represent differance in bytes beetwen whence and new location.Whence represent a file pointer location and have 3 possible values:

SEEK_SET seeks from begining of the file
SEEK_CUR seeks from current location and
SEEK_END seeks from EOF.

Savage
Apr 27 '07 #8
Sharkolomew
5 New Member
Well turns out append mode is not what I am looking for. I need to insert or replace text on the current line, not at the end.

To get to the current line, I use while(f>>input) //input is an int\

Once it finds the line, I want to add to that current line and keep the rest of the file intact.

Fseek looks like it may work, but I cant find a relevant example in replacing text when I googled it.
Apr 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3347
by: dansan | last post by:
I have been tryning to make some of our C# programs talk to some perl programs. These perl jewels output in binary. I have been trying to use Process.StandardOutput to read the output from the...
7
2605
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
1
8336
by: michael8 | last post by:
The problem was to write a program that reads a .cpp file containing a C++ program and produces a file with all comments stripped from the program. I finally got the answer. Here it is if...
2
7358
by: Ed | last post by:
Hope someone can help me out... I have been tasked to read some image data from an sql database and save the files to flat files. OK, sounds easy as I'v used BLOBs before. But this is an old...
21
626
by: Naya | last post by:
Hello, everyone!!! Well, I have a situation here. I am trying to read this data from a file, but the wrong values keep spitting out at me. Here's what I mean: Program: int main() {...
1
2306
by: anii | last post by:
Hi! I'm working on a piece of code for school. =) in C++. (I've read the rules and understand that i can't put all of the code here and ask for all of the code) I've got all (or most) of the code...
3
1625
by: newguy194 | last post by:
I have written the skeleton for a program which is supposed to dissect a text file by first getting the entire contents of the textfile as a string, then searching that string for a matching first...
21
3013
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
1
1870
intOwnsVoid
by: intOwnsVoid | last post by:
This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream> #include <fstream> using namespace std;
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.