redsox Posted July 25, 2007 Share Posted July 25, 2007 I am currently writing a program to read in certain values from a text document, modify the values, and output the answer onto the screen. Here is the code that I found online with which I am reading the file. char str[5]; fstream file_op("d:20060914-152330.txt",ios::in); while(!file_op.eof()) { file_op.getline(str,5); cout <<str; } file_op.close(); cout <<endl; Can someone please tell me what the 5 represents? I thought it represented the number of characters the program would read from the file, but I am not too sure about it. Here is the text document that I am trying to read. 38973.82499 0.77609 1392 1490 827 865 1709 1315 850 1398 1493 829 866 1712 1329 860 0 0 0 0 0 0 0 38973.82569 0.77436 1388 1485 824 861 1703 1308 842 1395 1486 825 862 1704 1326 857 0 0 0 0 0 0 0 38973.82638 0.77261 1379 1474 819 857 1695 1305 829 1386 1473 818 856 1693 1319 839 0 1 0 1 2 0 0 38973.82708 0.77085 1375 1467 816 854 1689 1301 815 1381 1468 816 854 1690 1317 826 0 0 0 0 1 0 0 38973.82777 0.76907 1369 1462 813 851 1684 1296 805 1376 1462 813 851 1684 1313 819 0 0 0 0 0 0 0 38973.82847 0.76728 1363 1456 810 848 1678 1294 803 1373 1457 811 848 1678 1309 815 0 0 0 0 0 0 0 38973.82916 0.76548 1363 1457 810 848 1677 1292 803 1370 1457 810 848 1678 1307 816 0 0 0 0 0 0 0 38973.82986 0.76366 1362 1457 810 847 1675 1290 810 1371 1458 811 847 1677 1306 823 0 0 0 0 0 0 0 38973.83055 0.76183 1361 1456 809 846 1673 1288 810 1368 1456 810 845 1673 1303 821 0 0 0 0 1 0 0 38973.83125 0.75998 1357 1451 807 844 1668 1284 802 1363 1451 807 843 1668 1300 814 0 0 0 0 0 0 0 38973.83194 0.75812 1348 1439 801 839 1660 1280 782 1354 1438 800 838 1659 1295 792 0 0 0 0 1 0 0 38973.83263 0.75625 1340 1430 797 834 1652 1274 772 1347 1430 796 834 1652 1290 785 0 1 0 0 0 0 0 38973.83333 0.75436 1337 1426 795 833 1649 1274 771 1344 1427 796 833 1651 1286 782 0 0 0 0 0 0 0 38973.83402 0.75246 1334 1424 794 830 1644 1267 773 1343 1425 794 831 1646 1283 785 0 0 0 0 0 0 0 38973.83472 0.75054 1331 1422 792 828 1640 1264 772 1338 1422 792 828 1641 1280 784 0 1 0 1 1 0 0 38973.83541 0.74861 1330 1422 792 828 1638 1261 773 1337 1422 792 828 1640 1278 785 0 0 0 0 1 0 0 38973.83611 0.74667 1323 1413 787 823 1631 1257 762 1330 1414 788 825 1633 1273 772 0 0 0 0 0 0 0 38973.83680 0.74472 1315 1403 782 819 1622 1251 751 1323 1405 783 820 1625 1268 761 0 0 0 0 0 0 0 38973.83750 0.74275 1305 1388 775 813 1612 1247 730 1312 1389 775 813 1613 1260 741 0 0 0 0 0 0 0 38973.83819 0.74076 1303 1386 774 812 1610 1244 726 1309 1386 774 812 1610 1258 737 0 0 0 0 0 0 0 38973.83888 0.73877 1298 1378 770 808 1603 1239 722 1306 1379 770 809 1605 1255 733 0 0 0 0 0 0 0 38973.83958 0.73675 1294 1373 768 806 1600 1238 717 1300 1374 768 807 1600 1251 727 0 0 0 0 1 0 0 38973.84027 0.73473 1291 1371 767 805 1596 1234 710 1296 1371 767 805 1595 1248 721 0 1 0 0 1 0 0 38973.84097 0.73269 1283 1361 762 800 1588 1230 700 1291 1362 762 800 1590 1245 710 0 0 0 0 0 0 0 38973.84166 0.73064 1279 1352 757 797 1582 1228 696 1284 1352 758 797 1583 1240 706 0 0 0 0 0 0 0 38973.84236 0.72858 1278 1353 757 796 1580 1224 702 1284 1351 757 796 1579 1239 713 0 1 0 0 1 0 0 38973.84305 0.72650 1282 1363 762 798 1584 1225 720 1290 1364 763 799 1585 1238 730 0 0 0 0 1 0 0 38973.84375 0.72441 1279 1361 761 797 1581 1222 712 1285 1361 760 796 1580 1236 722 0 1 0 0 1 0 0 38973.84444 0.72231 1273 1356 758 794 1575 1218 707 1281 1356 758 794 1575 1231 717 0 0 0 0 0 0 0 38973.84513 0.72019 1269 1352 756 791 1569 1213 702 1275 1352 756 791 1570 1224 711 0 0 0 0 0 0 0 38973.84583 0.71806 1262 1344 752 786 1560 1206 696 1267 1345 752 786 1561 1219 704 0 0 0 0 0 0 0 38973.84652 0.71592 1254 1334 746 782 1552 1201 685 1259 1335 747 782 1553 1211 692 0 0 0 0 0 0 0 38973.84722 0.71376 1246 1324 741 777 1542 1194 672 1252 1325 742 778 1543 1206 681 0 0 0 0 0 0 0 I want to take the values from the second and third column and use them to calculate a particular output. The output will be just one particular value. My question is how do I grab those two particular columns. I am assuming that I have to specify for the program to look for a space to differentiate between the two columns. But I am not sure as to how I can do that. Any help would be very much appreciated. Thank you. Link to comment Share on other sites More sharing options...
Ghostrider Posted July 25, 2007 Share Posted July 25, 2007 You are right in your assumption that the "5" represents the number of characters. Check this site for some tips on cleaning up that code. I'm not entirely sure that you want to use a eof(). But if it works for you, go for it. Link to comment Share on other sites More sharing options...
redsox Posted July 25, 2007 Author Share Posted July 25, 2007 Thanks for the reply. I am a novice in programming, so basically I just go with what I find online. Also, what site are you referring to - this MVPMODS site? Do you know how I can store just the two columns that I need for my calculations? It would be nice if the program would read the file and display the data but I am more interested in storing the said columns to a variable (maybe an array? not sure) and use that variable to make my changes. Thank you for the help. Link to comment Share on other sites More sharing options...
Ghostrider Posted July 25, 2007 Share Posted July 25, 2007 Thanks for the reply. I am a novice in programming, so basically I just go with what I find online. Also, what site are you referring to - this MVPMODS site? Do you know how I can store just the two columns that I need for my calculations? It would be nice if the program would read the file and display the data but I am more interested in storing the said columns to a variable (maybe an array? not sure) and use that variable to make my changes. Thank you for the help.Crap, I forgot to link the site in my previous post. http://cboard.cprogramming.com/showthread.php?t=69403 It seems like that thread talks about something similar to what you are trying to do. Link to comment Share on other sites More sharing options...
ronmexico Posted July 25, 2007 Share Posted July 25, 2007 You can use a regex if you are willing to put it in this form 'the column info' It's the simplest thing I thought of, but I'm sure I'll think of something if I have a moment to think about it, so off to lunch then. Link to comment Share on other sites More sharing options...
ronmexico Posted July 25, 2007 Share Posted July 25, 2007 Good news, I didn't get to lunch before I thought of something, since all your columns start in the same place on every line, you can start reading from a certain character # on a line (like read the 10th to 15th and then the 17th to the 22nd) and use that data. Link to comment Share on other sites More sharing options...
redsox Posted July 25, 2007 Author Share Posted July 25, 2007 Good news, I didn't get to lunch before I thought of something, since all your columns start in the same place on every line, you can start reading from a certain character # on a line (like read the 10th to 15th and then the 17th to the 22nd) and use that data. Thanks a bunch for that. That is exactly what I am looking to do. Would I use the getline command to do that? You had mentioned that I could just read the 10th to 15th characters etc., but how would that work if I have more rows. At the moment, I have about 33 rows but other files could have more than 33 rows (more like 50 or even in the 100s). Or does that not matter. I guess it really wouldn't matter how many rows I have since it will always be looking for the 10th to 15th characters and the 17th to 22nd characters on any row, correct? Thanks for the help. @ Ghostrider - Thanks for the link. I will take a look at the link that you had provided me with. Take care. Link to comment Share on other sites More sharing options...
ronmexico Posted July 25, 2007 Share Posted July 25, 2007 Not too sure about the getline command question because I haven't done C++ in a while and I kinda don't like the API of it, so it will take me a bit to find it most probably. Secondly, it doesn't matter how many rows because what you should be doing anyways is looping it like so: while(getline!=null) { getline } I looked at your code and found that the eof function does just that,t eh getline!=null so you can keep the eof loop. Link to comment Share on other sites More sharing options...
redsox Posted July 25, 2007 Author Share Posted July 25, 2007 Thank you. I will give it a try. Take care. Link to comment Share on other sites More sharing options...
ronmexico Posted July 25, 2007 Share Posted July 25, 2007 Thought of something much simpler. substr(size_type pos, size_type n) meaning substr(place you start, how long) Heres an example: string str18 = "abcdefghi" string str19 = str18.substr (6,2); cout << str19 << endl; // prints out "gh" initialize i to 0 or 1, 1 if line numbers start at 1 in C++ 0 if they start at 0. while(!file_op.eof()) { file_op.getline(str,i); cout <<str.substr(start point, how many chars); i++ } file_op.close(); cout <<endl; That's the code to get it to print out the substring for whatever you wanted it as. Link to comment Share on other sites More sharing options...
redsox Posted July 25, 2007 Author Share Posted July 25, 2007 Thank you very much. BTW, I would have to define my variable as a floating point variable correct (instead of a string variable)? Link to comment Share on other sites More sharing options...
ronmexico Posted July 25, 2007 Share Posted July 25, 2007 No, you can only take the substring of a string. Also, since you are just printing the numbers, you don't need to make them anything but a string. Link to comment Share on other sites More sharing options...
redsox Posted July 26, 2007 Author Share Posted July 26, 2007 I am having some trouble getting this to work right. Here is my modified code based on your example. ifstream indata; // indata is like cin char str[5000]; fstream file_op("d:20060914-152330.txt",ios::in); char i=0; while(!file_op.eof()) { file_op.getline(str,i); cout << str.substr (12,7); i++; } file_op.close(); cout <<endl; I am getting the following error: error C2228: left of '.substr' must have class/struct/union type Error executing cl.exe. I think this is due to the fact that I have defined str as a char but I am not able to say "string str=0;" - to initialize. I have included the <string.h> header file. Is there anything more I need to add? Thank you. Link to comment Share on other sites More sharing options...
ronmexico Posted July 26, 2007 Share Posted July 26, 2007 I didn't notice the char [] before, why not just make it a string? Heres some help on that subject if need be http://www.mochima.com/tutorials/strings.html Link to comment Share on other sites More sharing options...
redsox Posted July 26, 2007 Author Share Posted July 26, 2007 Thats what I planned on doing but I wasn't able to define the string. Now I know that I had included the wrong header file. Thanks for the link and for your help. I will post as I move along in this program. Link to comment Share on other sites More sharing options...
redsox Posted July 30, 2007 Author Share Posted July 30, 2007 I had modified my code to resemble this, but I am still not able to get it to work. I am not able to view anything on the screen. I just get a black command box. Here is the modified code. string str; ofstream myfile ("20060914-152330.txt"); while(myfile.is_open()) { cin >> str; getline (cin,str); cout <<str; } myfile.close(); cout <<endl; Thanks for anyone who can help me with this. Please keep in mind that I am novice at c++. I just know the very basic information and nothing more. Take care. Link to comment Share on other sites More sharing options...
ronmexico Posted July 30, 2007 Share Posted July 30, 2007 Well you only get stuff in the command prompt because you didn't write anything that would make it visual. You have to create GUI's n such for it to work with a screen of sorts. I've taken up learning Objective-C (a language commonly used for Mac Cocoa applications) and I don't know much about making GUI's because it is a similar style to VB.net GUI making. cout prints to command line for the record. Link to comment Share on other sites More sharing options...
redsox Posted July 30, 2007 Author Share Posted July 30, 2007 Thank you very much for the reply. I wouldn't have to create a GUI to just display some values in a command box correct? I thought the "cout" command would print what ever you want to be printed on to the black command box that pops up when your run the code. I was actually trying to debug and found out that it has to do with the way I am opening the file. I added the following code before: ofstream myfile ("20060914-152330.txt"); and I was able to see "Just a test". cout << "Just a test" << endl; I am really not sure as to how can fix the problem. Thank you very for your help. Link to comment Share on other sites More sharing options...
ronmexico Posted July 31, 2007 Share Posted July 31, 2007 I'll look it up next time I get an internet connection, sometime hopefully within this week in Norway. Link to comment Share on other sites More sharing options...
redsox Posted July 31, 2007 Author Share Posted July 31, 2007 Thanks a lot for your help ronmexico. To give you an update. I was able to read in the whole file by using the following set of commands. string str; ifstream myfile ("20060914-152330.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,str); cout << str << endl; } myfile.close(); } else cout << "Unable to open file"; And I was able to output the first column I need by using the following code: cout <<str.substr(12,7) << endl; i++; The final step I need to do is to take the values in the particular column I had read and store it so that I can make some changes. For example, I would like to take the inverse cosine of all the values in that particular column and display the acquired result. I would very much be thankful if someone could answer that final part for me. Take care. Link to comment Share on other sites More sharing options...
redsox Posted July 31, 2007 Author Share Posted July 31, 2007 I got it! Thanks for all the help. I had to define another variable and use "myfile" to read and store the column data in a variable. I just used the following commands: myfile >> column1 >> column2 >> column3; As of this moment, everything seems to be working great. Hopefully the whole program will run smoothly. Take care. Link to comment Share on other sites More sharing options...
ronmexico Posted August 1, 2007 Share Posted August 1, 2007 Good job and good luck, I found wifi in some small wonderful town, which I was attracted to not because of the wifi, but the fact that it was 100 KM out of Alta, which means I have a short trip tommorow after a 900 KM drive. It looks good to me and nice coding if I may say so myself ;) Link to comment Share on other sites More sharing options...
redsox Posted August 1, 2007 Author Share Posted August 1, 2007 Sounds like you are having a nice road trip. And thanks for all the help. Everything seems to be running smoothly now. Take care. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.