How to print all the contents of a file without eof(); or vectors or classes.

Pages: 12
Feb 3, 2021 at 9:39pm
I have a project to submit the day after tomorrow, that is before midnight tomorrow. Before that, our Programming Fundamentals Incharge gave us a brief go-through the concept of filing which is mandatory in the project.

In his code, he printed out the contents of a .txt file using the following code.

1
2
3
4
while(ff>>s.rollno>>s.name)
{
cout<<endl<<s.rollno<<"\t"<<s.name;
}


and it worked... where as my code is based on the same concept but has slghtly more complicated structures of strings to store each word of each line in every iteration of the file.

1
2
3
4
  while(print_candidate>>candidate.formno>>candidate.personal.namef>>candidate.personal.namel>>candidate.data.category>>candidate.data.group>>candidate.data.marks)
{
cout<<endl<<candidate.formno<<"\t"<<candidate.personal.namef<<"\t"<<candidate.personal.namel<<"\t"<<candidate.data.category<<"\t"<<candidate.data.group<<"\t"<<candidate.data.marks;
}



my code simply does not print a single character. and doesn't even give any errors.
furthermore, by using my teacher's method.. i want to be able to give a formatted output. that's why i m going for his method and not eof(); at least.


P.S,; I am new here and i'm trying to format this question better but the buttons don't seem to work, ii hope you understand.. Also, Classes and Vectores are not allowed since they are 'Out-of-Course' so far..
Last edited on Feb 3, 2021 at 9:49pm
Feb 3, 2021 at 9:50pm
You need to show exactly what your struct looks like and exactly what your input looks like.

You also need to ensure that the file opened properly:

1
2
3
4
5
6
    ifstream fin("filename");
    if (!fin)
    {
        cerr << "Cannot open input file.\n";
        exit(1);
    }

(BTW, "genius" doesn't have an 'o' in it ... but maybe it's a joke, in which case it's kind of funny.)
Last edited on Feb 3, 2021 at 9:56pm
Feb 3, 2021 at 9:52pm
Can you post an example of the input file?

Are you sure that you're opening the input file successfully?
Feb 3, 2021 at 9:53pm
The file is .txt formatted to look like a table. and the columns are the same as in cout statement.

Although, now that i think about it... some of these structure variables such as formno is an int while ifstream print_candidate("candidates.txt",ios::in); seems to allow to read data only as string. is that an issue?
Feb 3, 2021 at 9:54pm
well, i know the spellings bruhh.. it's just my username. ;)
Feb 3, 2021 at 9:54pm
yes, the file is opening without an issue.. i have a heck lot of error checking.
Feb 3, 2021 at 9:56pm
FormNo First Name Last Name Category Group Marmyks
10028 Muhammad Zubair Muhammad Abdullah engg AS 1010
10044 Muhammad Abdullah Abdul Rehman engg army 898
10268 Usama Khan Saad ur Rehman cs civ 812
10302 Muhammad Kamran Warmyis khan cs civ 937



here's a sample of the file.. and yes, dutch.. i have put that error check
p.s; Due to the formatting of the replies, it converted the tab_spaces into normal single_spaces. (keep that in mind)
Last edited on Feb 3, 2021 at 10:00pm
Feb 3, 2021 at 9:59pm
This is my teacher's complete code that prints the contents of a .txt file (formatted as a table) with only 2 columns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void showdata(student s)
{
    cout<<endl<<s.rollno<<"\t"<<s.name;
}
void readdata()
{
    ifstream ff("record.txt",ios::in);
    if(!ff)
    cout<<"\n Error:file not found";
    else
    {
        student s;
        char h1[10];
        char h2[20];
        ff>>h1>>h2;
        cout<<endl<<h1<<"\t"<<h2;
        while(ff>>s.rollno>>s.name)
      
            showdata(s);
            
        
    }
Feb 3, 2021 at 10:02pm
please respond.. i only have 1 day left and i haven't even begun working on the main function of the project, merit based list generation. :(
Feb 3, 2021 at 10:13pm
Is the first line "FormNo First Name Last Name Category Group Marmyks" actually in the file?

Where exactly are the tabs in the input file? I cannot parse the names since I am not familiar with them. I presume something like this:

1
2
3
4
5
FormNo<TAB>First Name<TAB>Last Name<TAB>Category<TAB>Group<TAB>Marks
10028<TAB>Muhammad Zubair Muhammad<TAB>Abdullah<TAB>engg<TAB>AS<TAB>1010
10044<TAB>Muhammad Abdullah Abdul<TAB>Rehman<TAB>engg<TAB>army<TAB>898
10268<TAB>Usama Khan Saad<TAB>ur Rehman<TAB>cs<TAB>civ<TAB>812
10302<TAB>Muhammad Kamran Warmyis<TAB>Khan<TAB>cs<TAB>civ<TAB>937

That complicates things.

And you still haven't shown me your struct.
Last edited on Feb 3, 2021 at 10:15pm
Feb 3, 2021 at 10:20pm
This is my Structure;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        struct name
    	{
    		string namef;
    		string namel;
	};
	struct info
	{
		int marks;
		string group;
		string category;
	};
    	struct form
    	{
    		int formno;
    		int session;
    		name personal;
    		info data;
    	};
    	form candidate;



As for the file....
1
2
3
4
5
FormNo<TAB>First Name<TAB>Last Name<TAB>Category<TAB>Group<TAB>Marks<NEW_LINE>
10028<TAB>Muhammad Zubair<TAB>Muhammad Abdullah<TAB>engg<TAB>army<TAB>1010<NEW_LINE>
10044<TAB>Muhammad Abdullah<TAB>Abdul Rehman<TAB>engg<TAB>army<TAB>898<NEW_LINE>
10268<TAB>Usama Khan<TAB>Saad ur Rehman<TAB>cs<TAB>civ<TAB>812<NEW_LINE>
10302<TAB>Muhammad Kamran<TAB>Waris Khan<TAB>cs<TAB>civ<TAB>937<NEW_LINE>



Yes, the top line labelled the table and specifies the columns for the user's understanding and also prints out when asked to print all the data.
But no, it will obviously not be used in merit list generation later on while the data underneath will be later on.


Last edited on Feb 3, 2021 at 10:25pm
Feb 3, 2021 at 10:36pm
Try this:

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

struct name
{
    string namef;
    string namel;
};

struct info
{
    int marks;
    string group;
    string category;
};

struct form
{
    int formno;
    int session;
    name personal;
    info data;
};

int main()
{
    ifstream fin("candidates.txt");
    if (!fin)
    {
        cerr << "Cannot open input file.\n";
        exit(1);
    }

    // Read the header line.
    string header;
    getline(fin, header);
    cout << header << '\n';

    form candidate;

    while (fin >> candidate.formno)
    {
        // Need to read these based on tabs since they can contain spaces.
        getline(fin >> ws, candidate.personal.namef, '\t');
        getline(fin >> ws, candidate.personal.namel, '\t');

        // These do not contain spaces.
        fin >> candidate.data.category
            >> candidate.data.group
            >> candidate.data.marks;

        cout << candidate.formno         << '\t'
             << candidate.personal.namef << '\t'
             << candidate.personal.namel << '\t'
             << candidate.data.category  << '\t'
             << candidate.data.group     << '\t'
             << candidate.data.marks     << '\n';
    }
}

About this:

 
        getline(fin >> ws, candidate.personal.namef, '\t');

The ">> ws" part after "fin" skips initial whitespace.
The "getline" with a final argument of '\t' means to read up to the next tab (removing the tab from the stream but not including it in the returned string).

Note that the output will not line up perfectly just using tabs.

BTW, you have mixed spaces and tabs in your code indentation. You need to stick with all one or the other. It is usually best to just use spaces. Four spaces for each indentation level is enough.
Last edited on Feb 3, 2021 at 10:38pm
Feb 3, 2021 at 10:52pm
okay, i'll try.. rn, i'm working on a function to create the necessary files if they don't exist.

Well, txt editor has it old specified tab spaces/width of 8 spaces. and if the first word occupies 7 spaces the second word in the next tab will not indent to the next one. Try it yourself.. i was kinda disappointed by that too.
Feb 3, 2021 at 11:54pm
IT WORKED! Thanks ALOT! :D
Feb 3, 2021 at 11:56pm
Btw, check this out..
https://www.cplusplus.com/forum/general/275891/

I will try it myself as well for any future projects of my own or if anyone else wants to person a similar function within that program? :D
Feb 4, 2021 at 1:41am
oo I know :)
system("type filename"); //win
or
system('cat filename'); //linux

when limited from using anything, go rogue.
Last edited on Feb 4, 2021 at 1:44am
Feb 4, 2021 at 1:14pm
what does it do though? @jonnin
and what's meant by 'type' and 'filename'?
does the file name need to be in ""?
does it need to include it's extension in the end?
Last edited on Feb 4, 2021 at 1:15pm
Feb 4, 2021 at 2:29pm
system() invokes the string parameter it is given (this is why its in "", but you actually would cook up a string variable to put in there if the file names are not constants). It does exactly what would happen if you typed the string at the command prompt, outside of C++.
type is a windows/dos command that displays a text file to the screen.
cat is a unix command that displays a text file to the screen.

yes, filename would need its extension and path if not in the 'current directory'.
something like this:
int main(char* argv, int argc)
{
string s = "type ";
s+= argv[1];//the 0th is the program's name
system(s); //use the string we built
}
call that:
c:\> program c:\folder\textfile.txt
and it will print the contents
-- this is exactly the same as
c:\> type c:\folder\textfile.txt


This was really just a joke because of the heavy limitations on the assignment. System is frowned upon in real code (it has security problems you can look up); but you can use it to make a 'for me only' type utility program or other 'at home' toys. If you get along well with your professor, it might get a laugh, if not, it might also get an F/ 0 score :P I don't advise turning it in, but when both hands are tied behind your back...
Last edited on Feb 4, 2021 at 2:36pm
Feb 4, 2021 at 2:35pm
Ah, okay.. Speaking of, I've been trying to use that argv and argc and am unable to understand a thing.. Do you know any good site that explains it well briefly with examples? Other than this one though, I haven't learned enough to understand what <char*>(&str) etc means..
Feb 4, 2021 at 3:37pm
Hey! @dutch
Can you help me out with this?
http://cplusplus.com/forum/beginner/275904/


Also... in the above example when you read candidate.data.marks which is an int value, can i use it with a relational operator within conditions after reading&stoing into it from a file?

previously in password change function, when i tried that.. reading a word returned string value and I had to convert it using stoi(); first.
Last edited on Feb 4, 2021 at 4:22pm
Pages: 12