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.
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.
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..
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?
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)
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;
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.
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.
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.
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?
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...
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..
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.