473,404 Members | 2,170 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,404 software developers and data experts.

Reading/Writing data from .csv file

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 10723
gpraghuram
1,275 Expert 1GB
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
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,12345,&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
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...
4
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 =...
2
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...
3
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...
9
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...
8
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...
5
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...
6
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
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...
21
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.