473,651 Members | 3,068 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,3 4" 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 1544
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,3 4" 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
3369
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 perl programs. I am able to read the output if it is ASCII (or some char of some sort). But the issues comes when it is binary info. I have tried to use StandardOutput.BaseStream, which returns a Stream to then read the bytes out of that...
7
2613
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 string will often not produce a direct match, the results will be based on proximity (50%, 20% 100%, etc). are there any good examples out there on how to do keyword searches on XML data? How should i set up my xml file so
1
8364
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 someone want it as no one could do it : P #include <iostream>
2
7377
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 database and I cannot get the image to work. The columns in the database are of type text. Here is one of the images text (in full) in the database (I hope you can see it):
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() { ifstream inFile;
1
2325
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 done. I'm having trouble with the output. I'm supposed to: Enter a csv file (with info in it) Read the file Go through it one by one and sort it into a structure Sort it by the customer id (which is also used in the csv file)
3
1632
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 word, then skipping over a delimiter and returning the correct word. For some reason it does not return anything, and I do not see where my error is occuring. #include <cstdlib> #include <iostream> #include <fstream> #include <conio.h> using...
21
3046
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
1
1875
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
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.