473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reading and displaying lines from file.

Whats up guys. I just decided i would try porgramming as i am a physical education major. As apart of my major i deal with alot of paper work. So i wanted to write a program to mess with all the papers i have to deal with. My question is i have read in my text file and can display the WHOLE text file. Now i want to be able to look at individual lines of my text file to see if they meet a certain criteria. can anyone help me with this problem i have this code so far :
Expand|Select|Wrap|Line Numbers
  1. string line;
  2.   ifstream myfile ("C:\\Documents and Settings\\ProbablyNotAGoodIdeaToPostYourFullNameOnTheInternetMike\\Desktop\\test.txt");
  3.   if (myfile.is_open())
  4.   {
  5.     while (! myfile.eof() )
  6.     {
  7.       getline (myfile,line);
  8.       cout << line << endl;
  9.     }
  10.     myfile.close();
  11.   }
  12.  
  13.   else cout << "Unable to open file";
Nov 1 '07 #1
17 2910
sicarie
4,677 Expert Mod 4TB
Okay, so what is the criteria?
Nov 1 '07 #2
GGnOrE
8
Expand|Select|Wrap|Line Numbers
  1. char *gets (buffer)
  2. printf("\n");
  3.  
Reads in lines of code from the file until a newline is reached, but the newline isn't stored. So maybe you want a text string for one whole line, then add new line. Repeat those steps, over and over!!!
It might work.
Nov 2 '07 #3
im sorry im really new to all this can u suggest how i use the char *gets (buffer) and stuff in my code or where i would put it or implement it in the code.
Nov 2 '07 #4
sicarie
4,677 Expert Mod 4TB
I would worry about the criteria first, you already have the lines split up by \n, as that is what getline()'s delimiter is.

Once again, what is the criteria you want to look for in each line?
Nov 2 '07 #5
if the line begins with a number then i will send that line to a specific function
Nov 2 '07 #6
sicarie
4,677 Expert Mod 4TB
So there are two ways to do this. The first way is to create a regex that will look for a number there, or if there is a delimiter (such as a space or a period after each number - that (this is important) will be there every time - you can use something like strtok() to parse that line.

Regex's are much more heavy a load on the computer (depending on how they are used), but have some interesting features such as grouping, etc... That will require you to include another library and do some interesting things like figure out the exact regex you want, compile it, and run it against each string.

Strtok is a bit easier, but you have to make sure that delimiter is there. There are others besides strtok(), but I can't remember them right now as it's what I most commonly use...
Nov 2 '07 #7
Im still not exactly sure how i go about this.. im sorry im really new to c++ programing. I need to place a single line out of the text file into an array.. sorry ... thanks for any help
Nov 4 '07 #8
oler1s
671 Expert 512MB
We don’t exactly understand what you want to do. Programming is about precision, and your problem description is too vague. This is what I believe I have understood so far. You have a text file, that consists of a number of lines. You want to process the text file line by line, specifically looking for lines that start with a number. Lines that start with a number are going to be processed in their entirety by another part of your code. Is this correct so far?

We might as well start with getting each line first. In C++, the most convenient way is to use a string and the getline method. From what I can see, you understand how to open the file. However, your use of eof is incorrect. Most people think the code they write means “if you haven’t reached the end of the file, read from it”. In reality, the code says if the EOF flag isn’t set, read from the file. What they don’t realize is that the EOF flag isn’t set until a failed I/O operation. See the problem? (If you don’t, ask and I’ll explain further).

Functions have return values. Return values come in handy. Like in loops. For example, if you do something like while (getline (...)), the effect is that you test if getline worked or not. If getline hits an EOF, the while loop will fail.

So let’s start with getting file input into strings correctly. Show us code that extracts line by line from the file into a string.
Nov 5 '07 #9
Thats my problem im not sure how i go about extracting line by line into strings... ex. i have a text file scores.txt


125
123
189
156
198


i want to take each of these lines and store them into an array...

getline is a little confusing as well ....

thanks guys i appreciate you taking the time help a newbie
Nov 5 '07 #10
I have managed this much so far. The following code looks at my text file and places ONLY the first character of each line in the array " char arry[10]" . So all i really need to know how to do is " how do i place every character on any given line into the array place holder" and next " how do i ignore white space and letters as it looks at my text file." sorry if this is confusing

ex. textfile.txt contains
126
220
325
429
538

my code only places the number 1 in the "char arry[10] and then moves to the next line and places the number 2 in the "char arry[10] and so on.


Expand|Select|Wrap|Line Numbers
  1.  
  2. char arry[10];
  3.  
  4.     ifstream in ("C:\\Users\\user\\Desktop\\test.txt");
  5.  
  6.   if(!in) { 
  7.     cout << "Cannot open file.\n"; 
  8.     return 1; 
  9.   } 
  10.  
  11.   in >> arry[0]; 
  12.   in >> arry[1]; 
  13.   in >> arry[2]; 
  14.   in >> arry[3];
  15.   in >> arry[4]; 
  16.  
  17.   cout << arry[4] << " " << arry[3] << " " << arry[2] << endl; 
  18.  
  19.  
  20.   in.close(); 
  21.  
  22.  
thanks guys for any help
Nov 5 '07 #11
sicarie
4,677 Expert Mod 4TB
You've declared the array as a char. This means each spot will hold a single character. What you want to do is use either strings (as mentioned above) or char* arrays.
Nov 5 '07 #12
thanks... ok so now how would i go about filtering out characters such as letters in my text file so that when the file is read it will ignore letters and white space, basically only read numbers
Nov 5 '07 #13
sorry that does not help
Nov 5 '07 #14
sorry guys i got one more new question... how can i check to see if the line that is read in starts with a number or a letter. if the line starts with a number then proceed but if the line starts with a letter or other character other than a number then skip that line. thanks in advance
Nov 8 '07 #15
I'm still relatively inexperienced at C++ too, but one way I can think of off the top of my head to check if the first character on each line is a number or letter would be to use a switch statement that says something to the effect of

Expand|Select|Wrap|Line Numbers
  1. switch(firstCharacter)
  2. {
  3. case "0":
  4. case "1":
  5. case "2":
  6. etc
  7. case "9": { };break //store the line or whatever you're doing with it
  8. default: break;
  9. }
That may not be the best way to do it, but it's one way I can think of....if there is a better way, someone share it because it's something I'd like to know too.


Edit: The default case would be if the first character is a letter (or anything other than a number, for that matter) in which case it would drop out of the switch and go on to whatever is next. I'm assuming that would probably be the end of a loop, and after dropping out with the break, it would loop back up and do the next line.
Nov 8 '07 #16
oler1s
671 Expert 512MB
grmorris, your idea, while it may work, is not exactly what we would suggest.

If you have stored the line in a C++ string. Indexing the string at 0 would give you the first character. To check if it is a number, use the isdigit() function in <cctype> .
Nov 8 '07 #17
grmorris, your idea, while it may work, is not exactly what we would suggest.
I don't mind, that's the whole reason I came here to begin with, to bounce my horrible ideas off people who actually know what they're doing! Haha any code I post, anyone with legitimate experience is welcome to pick apart and tell me how to do it better...that's how you learn, listening to the experts
Nov 8 '07 #18

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

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
8
by: Jimbo | last post by:
I'm working on a win app that reads and processes each line of an ascii file until the end of the file. Since the file's 1.6 million lines long, after a while Windows displays the "Not Responding"...
6
by: zoneal | last post by:
Size of text file to read and display can be larger than 10MB. Since I must ignore certain input text lines the code I'm using is: FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input) 'Read...
12
by: mitek777 | last post by:
Hi, I have a problem with reading from file. I would like to find some string(f e.g. name) in file, and if it exist show it on screen with second and third line under. I have this in file: ...
3
by: jerryyang_la1 | last post by:
Hi My mail server outputs live logs like this: T 20070325 123318 4605007c Connection from 111.111.111.111 T 20070325 123319 4605007c HELO testdomain.com T 20070325 125817 46050083 EHLO...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.