Record Display

Hi everyone, I have a question to ask you.

-I have to implement a C++ program to display records that are saved to a '.txt' file (via I/O redirection).

-The program takes the name of the data file as a command-line argument.

-The first record saved in the file has record number 1. The user specifies the records to display by entering an integer:
>If the user enters a positive integer, that integer is the record number of the record to display.
>If the user enters a negative integer, then its value specifies the first record to display and all records after are displayed.
>If the user enters 0, the program exits.

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
#include <iostream>
#define NameSize 20
#include <cstdlib>
#include <fstream.h>
using namespace std;

void readRecord(fstream file, int recordNum);

struct student
{
	char firstName[NameSize];
	char lastName[NameSize];
	int score;
}

int main(int argc, char *argv[]) //three conditions to make program stop. 1: reached the end of the main. 2: EXIT() 3: Program crashed.
{
	long recordNum=0;
	if (argc!=2)
		{
			cout<<"This is not valid. Please try again.";
			return EXIT_FAILURE;
		}	
	fstream file(argv[1],ios_base::in | ios_base::out | ios_base::binary); //ios_base::in is to read. ios_base::out is to write. ios_base::binary is to open the fil	e in binary mode.
		if (!file) //try to open file. if cannot oi
		{
			cout<<"File: "<<argv[1]<<" fileName"<<endl;
			exit(EXIT_FAILURE);
		}
	readRecord(file,recordNum);
	return 0;

}

void readRecord(fstream file, int recordNum)
{
	string line;
	student record;

	while(1)
	{
		cout<<"Enter the record number you wish to view: "<<endl;
	
		if(getline(cin,line)&& !cin.eof()) //GOOGLE. cin.eof to test if standard input has any errors. WHAT DOES ISTRINGSTREAM DO FOR US?
			//istringstream: characters can be inserted and/or extracted from the stream using any operation allowed on both input and output.
			//used to convert numbers to strings.
		{
			file.seekg(0);
			istringstream iss(line);
			if(iss>>recordNum)
			{
				if(recordNum==0)
					exit(EXIT_SUCCESS);
				else if(recordNum>0)
						   {
							streampos place=(recordNum-1)*sizeof(student); //what is 'streampos'?
						   }
				else {
					streampos place=(recordNum+1)*sizeof(student);
					}
			}	
		if(choice=='0')
			{
				return 0;
			}
		
	//	after reading the user's input, the program will read the records according to the input value, until the user tells it to stop.
	/*	if (choice<0)
			{
				while(1)
				{
					fseek(choice);
					cout<< //i dont know what to put here;

				}
			}

		if (choice>0)
			{
				
			}
	*/
		}
	}
}
//we are supposed to use the getline function to read the line. we are supposed to use 'fseek' and 'istringstream' and 'stringpos' and 'read'. to finish that program
//'fseek' is to find a specific spot in the file.

//'read' is to be able to read block by block if (argc==2). size of the student struct is fixed. it is efficient. 


I have gotten the "entering 0" and the checking if the input is positive or negative part done, but I don't know how to quite continue writing the code. It seems too confusing...

Thank you all in advance, I really appreciate it!
Topic archived. No new replies allowed.