473,770 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading/Writing data from .csv file

2 New Member
I'm a beginner in C++. I'm in the process of writing an 'Automated Library Management System' using C++ on a Windows machine. I'm stuck in the midst of reading/writing data from a .csv file that holds the information about materials available in the library.
Any help is greatly appreciated!
Jul 10 '07 #1
2 10740
gpraghuram
1,275 Recognized Expert Top Contributor
I'm a beginner in C++. I'm in the process of writing an 'Automated Library Management System' using C++ on a Windows machine. I'm stuck in the midst of reading/writing data from a .csv file that holds the information about materials available in the library.
Any help is greatly appreciated!
HI,
Till which point u have reached.?
And whats the area u are getting stuck?
Give more details so that people in the forum here can help u.

Raghuram
Jul 10 '07 #2
banerr2
2 New Member
HI,
Till which point u have reached.?
And whats the area u are getting stuck?
Give more details so that people in the forum here can help u.

Raghuram
I have rest of the prog working...but the reading/writing from .csv is still not up. I was writing the function separately.
Here's what I have till now:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.    ifstream infile("test.csv");
  8.    char c[100];
  9.    int counter=0;
  10.    while (infile >> c[counter]) 
  11.    {
  12.       counter++;
  13.    }
  14.  
  15.    cout << c << endl;
  16.    system("PAUSE");
  17.    return 0;
  18. }
  19.  
The output comes like:
John,Smith,1234 5,&James,Robert ,67890,&
Now there's two things to be done:
1. Change "," to " " and "&" to newline using a loop.
2. Search each candidate using their id no.

How can these be done?
Is there any better method than an array of char?
_______________ _______________ _________

I'm also attaching the rest of the program that have so far (in case you need it answer my question).
Expand|Select|Wrap|Line Numbers
  1. //ALMS Source code (Automated Library Management System) Version 1.0.0
  2. //Built using Dev C++ (version 5) with Mingw compiler on Windows Platform
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string.h>
  6. #include <windows.h>
  7. #include <fstream>
  8. #include <ctime> 
  9. #include <cstdlib>
  10. using namespace std;
  11.  
  12. //////Accessory fucntion to set colors///////////////////////
  13. void color(int i)
  14.  
  15. {    int a=0;
  16.  
  17.      if (i==0)//password (black text-black background)
  18.      {a=0;}
  19.      if (i==1)//normal (white text-black background)
  20.      {a=15;}
  21.      if (i==2)//error (red text-black background)
  22.      {a=12;}
  23.      if (i==3)//header (white text-maroon background)
  24.      {a=79;}
  25.  
  26.      HANDLE handle = GetStdHandle( STD_OUTPUT_HANDLE ); // Make a handle
  27.      SetConsoleTextAttribute( handle, a ); // Set the color of the text
  28. }
  29. //////////////Struct for ALMS//////////////////////
  30. struct library
  31. {
  32.        int isbn;
  33.        string subject, author;
  34. }text, video, reference;
  35.  
  36. ////////////////Quick Sort with Operator Overloading/////////////////
  37. /*
  38. template <class T>
  39. int partition(T data[], int size)
  40. {
  41.     T pivot = data[0];
  42.     int lo = 1;
  43.     int hi = size;
  44.     while (lo < hi) {
  45.         hi--;
  46.         while (data[lo] < pivot && lo < hi) 
  47.             lo++;
  48.         while (data[hi] > pivot && lo < hi) 
  49.             hi--;
  50.         if (lo < hi)
  51.             swap(data[lo], data[hi]);
  52.     }
  53.     if (lo > 0) {
  54.         if (data[lo] > pivot) lo--; 
  55.         swap(data[lo],data[0]);
  56.     }
  57.     return(lo);
  58. }
  59.  
  60. template <class T>
  61. void quicksort(T data[], int size)
  62. {
  63.     if (size > 1) {
  64.         int pivotloc = partition(data, size);
  65.         quicksort(data, pivotloc);
  66.         quicksort(data + pivotloc+1, size - pivotloc-1);
  67.     }
  68. }
  69. */
  70. ///////////////////
  71.  
  72. ////////About ALMS////////////////////////
  73. void about_ALMS (void)
  74. {
  75.      cout << "About ALMS:\n"
  76.              "ALMS (Automatic Library Maintainence System) is a freeware designed \nto aid libraries manage their material. "
  77.              "It is distributed under the \nCreative Commons License(BY-NC-ND). More details can be found at \nhttp://creativecommons.org/licenses/by-nc-nd/3.0/us/. "
  78.              "\nALMS is coded in C++ and compiled using Mingw compiler. ALMS has \nnot been tested on a wide range on machines. "
  79.              "Since it is a freeware, it is \ndistributed without any guarantee or liabilities.\n\n";  
  80. }
  81. ////////////Help topics about ALMS///////
  82. void help_ALMS (void)
  83. {
  84.      cout << "The Help section is still under construction\n";
  85. }
  86. ///////////////Random Id-number generator//////////////////
  87. int idgen(void)
  88.     srand((unsigned)time(0)); 
  89.     int random_integer; 
  90.     int lowest=1000000000, highest=1999999999; 
  91.     int range=(highest-lowest)+1; 
  92.     for (int i=0; i<20; i++)
  93.     {
  94.      random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)) + rand()%23; 
  95.      if (random_integer<0)
  96.      {
  97.        random_integer = random_integer*-1;
  98.      }         
  99.     } 
  100.     return random_integer;   
  101.  
  102. }
  103.  
  104. ////////////Logs user to their respective user accounts//////////////
  105. int account (int i)
  106. {
  107.      string s1, s2, input, greet="\nWhat would you like to do: \n(1) Login to your account \n(2) Register if you're a new user.\n";
  108.      string last, first, address, email;
  109.      int id;
  110.      if (i == 5)//Login to 
  111.      { 
  112.            s1 = "1";
  113.  
  114.      }
  115.      else if (i == 6)//edit info again
  116.      {
  117.           s1 = "2";
  118.      }
  119.      else //fresh user from another function
  120.      {
  121.           cout << greet;
  122.           cin >> s1;//temporary string to hold user entry
  123.      }
  124.      if (s1.compare("1") == 0)
  125.      {
  126.         cout << "Please enter your ALMS user id: ";
  127.         cin >> input; //Numbers put in string to eliminate program crashing in the case user types non-integer values
  128.         cout << input;
  129.  
  130.         //more things to come
  131.      }
  132.      else if (s1.compare("2") == 0)
  133.      {
  134.           id = idgen();
  135.           cout << "\nPlease fill the following fields:\n";
  136.           cout << "Name (Lastname Firstname):  ";
  137.           cin >> last >> first; 
  138.           cout << "Email:  ";
  139.           cin >> email;
  140.           cout << "Account id: " << id <<". \nYou'll need this account id to log into your ALMS account later. \nDO NOT share this number with anyone else.\n ";
  141.           cout << "\nPlease review the information above before submittion:\n";
  142.           cout << "(1) Submit information \n(2) Change account information\n  ";
  143.           cin >> s2;
  144.           if (s2.compare ("1") == 0)
  145.           {
  146.               account(5);
  147.           }
  148.           else if (s2.compare ("2") == 0)
  149.           { 
  150.               account (6); 
  151.           } 
  152.           else {}//automatically goes back to welcome screen
  153.      }
  154.      return 0;
  155.  
  156.  
  157. }
  158. ///////////distributes the task to different functions/////////////////// 
  159. void task (int i)//i passed from pass 
  160. {
  161.      string s1,s3;//temporary string variables 
  162.      string t[8] = {"\nWhat would you like to do: ", "Log into your account", "View help files", "Learn about ALMS", "Start over", "Modify/Add/Delete library material", "Search for user", "Retrieve user id"}; 
  163.      int j=0, count=0;//temporary variable used to store y/n value
  164.      s1.clear(); s3.clear();
  165.  
  166.      if (i == 1 || i==2)//for students and faculty
  167.      {
  168.            cout << t[0] << endl;
  169.            for (int n=1; n<5; n++)
  170.            {
  171.                count++;
  172.                cout <<"("<<count<<")"<<" "<<t[n]<< endl;
  173.            }   
  174.            cin >>s1;
  175.            if (s1.compare("1") == 0)//Log into account
  176.            {
  177.                account(i);
  178.                cout << endl;
  179.                task(1);
  180.            }    
  181.            else if (s1.compare("2") == 0)//HELP!!
  182.            { 
  183.              cout << "\n\n";
  184.              help_ALMS();
  185.              task (1);     
  186.            }
  187.            else if (s1.compare("3") == 0)//About ALMS
  188.            {
  189.               cout << "\n\n";
  190.               about_ALMS();
  191.               task (1);                  
  192.            }
  193.            else if (s1.compare("4") == 0)//Start over
  194.            {
  195.                s1.clear();
  196.            }
  197.  
  198.      }
  199.  
  200.      else if (i == 3)//staff
  201.      {
  202.  
  203.               cout << t[0] << endl;
  204.               for (int n=5; n<8; n++)
  205.               {
  206.                   count++;
  207.                   cout <<"("<<count<<")"<<" "<<t[n]<< endl;
  208.               }   
  209.               cin>>s3;
  210.               if (s3.compare("1") == 0)//Modify Add/Delete Library material
  211.               {}
  212.               else if (s3.compare("2") == 0)//Search for user
  213.               { }
  214.               else if (s3.compare("3") == 0)//Retrieve user id
  215.               { }
  216.               else {cout << "Invalid Entry. Please try again:\n"; task(i);}
  217.       }
  218.  
  219. }
  220. /////////////Password Function///////ONLY STAFF HAS TO GO THROUGH pass()/////////
  221. void pass (int i)
  222. {
  223.      if (i==1)
  224.      {task(1);}//student has no password check
  225.  
  226.      else if (i==2)
  227.      {task (2);}//faculty has no password check either
  228.  
  229.      else if (i == 3)
  230.      {
  231.           string passw, staff_pass="123", pass_error="Invalid staff password. ";
  232.           passw.clear();
  233.           cout << "Please enter staff password: ";
  234.           color(0);
  235.           cin >> passw;
  236.           color(1);
  237.           if (passw.compare(staff_pass) == 0)
  238.           {
  239.              task(3);
  240.           }
  241.           else 
  242.           {
  243.                string temp;//used to store y/n data
  244.                color (2);
  245.                cout << pass_error;
  246.                color (1);
  247.                cout << "Try again? (1) Yes (2)No\t";
  248.                cin >> temp;
  249.                if (temp.compare("1") == 0 || temp.compare("yes") == 0 || temp.compare("Yes") == 0)
  250.                {
  251.                     pass(3);
  252.                }
  253.                else {}//goes automatically back to main and continues from next statement
  254.           }
  255.       }
  256. }
  257.  
  258. ////////////Identifies the type of user accessing////////////////////
  259. void userid (string u)
  260. {
  261.    string s, greet="Welcome";//s is temporary storage for try again input  
  262.    s.clear();//clears the entry string during each run of userid
  263.    string names[6] = {"student", "Student", "faculty", "Faculty", "staff", "Staff"}; //There are two of each to make it case-insensitive            
  264.  
  265.    if (u.compare( names[0] ) == 0 || u.compare( names[1] ) == 0)
  266.    {
  267.         cout << endl << greet << " " << names[1] << endl;
  268.         pass (1);
  269.    }
  270.    else if (u.compare( names[2]) == 0 || u.compare( names[3] ) == 0) 
  271.    {
  272.         cout << endl << greet << " " << names[3] << endl; 
  273.         pass (2);
  274.    }
  275.    else if (u.compare( names[4] ) == 0 || u.compare( names[5] ) == 0)
  276.    {
  277.         cout << endl << greet << " " << names[5] << endl;
  278.         pass (3);
  279.    }
  280.    else
  281.    {
  282.     color(2);   
  283.     cout << "\nInvalid Entry. ";
  284.     color(1);
  285.     cout << "Try again? (1)Yes|(2)No\ ";
  286.     cin >> s;
  287.     if (s.compare("1") == 0)
  288.     {
  289.         s.clear();
  290.         cout << "\n\n";/*do noting and main automatically calls itself*/
  291.     }
  292.     else
  293.     {
  294.         s.clear();
  295.         u.clear();
  296.         cout << "\n";
  297.     }
  298.  
  299.     }
  300. }
  301. ////////////Main///////////////////////
  302. int main (void)
  303. {
  304.     color(3);//changes to header colors
  305.     string user, ALMS="ALMS (Automated Library Management System) [Version 1.0.0 ]";
  306.     cout << ALMS << endl; //Header file
  307.     color(1);//changes to default colors
  308.     cout << endl << "Please Login as: student | faculty | staff\t"; //Type of user
  309.     cin >> user;  
  310.     userid (user); //calls func userid() to identify the type of user
  311.     cout << "\n\n";
  312.     main ();//recursive main to enable continuous usability of ALMS
  313.     return 0;
  314.  
Jul 12 '07 #3

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

Similar topics

0
3592
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it is written ( commented out in code) then it reads in OK. However, when I move the code to the right place ( as shown here) it throws an IO exception. Both the Filter class and it's constituent class implement Serializable and it would seem the...
4
9842
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile = tmpfile(); /* write into tmpFile */ ...
2
3070
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
3
9512
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
9
2762
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the information from the text file! Can anyone enlighten me as to what I am doing wrong?
8
2672
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five different image files shuffled up in one big binary file. In order to view them I have to "unshuffle" the data, which means moving bytes around. Currently my approach is to read the data from the original, unshuffle as necessary, and then write...
5
5087
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I have it store info it gets from when the programs check in into a DataSet (XML file). Problem is, that file now has to be used by other programs to find out version information (the file is ALWAYS less that 1K.) The record itself is only five fields...
6
5273
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
42
4872
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong. If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data...
21
3064
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.