473,473 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getline();

38 New Member
Hello,

I am having trouble getting the getline() function in C++ to work I ahve read the references but I'm stilling having a hard time trying to get it to work can anybody help? thanks!

Expand|Select|Wrap|Line Numbers
  1.             FILE * pFile;
  2.                char du_sum[50];
  3.  
  4.             string check_dir = ("servers/" + cmd_svrid);
  5.             string du_dir = ("du -sb " + check_dir);
  6.  
  7.             pFile = popen(du_dir.c_str(), "r");
  8.  
  9.                 if (pFile == NULL) {
  10.  
  11.                     perror ("Error opening file");
  12.  
  13.                 } else {
  14.  
  15.                     fgets(du_sum, 100, pFile);
  16.  
  17.                     string du_string = du_sum;
  18.  
  19.                     find_and_replace(du_string, check_dir, "");
  20.  
  21.                     whitespace(du_string);
  22.  
  23.                     string total_bytes;
  24.  
  25.                     getline(du_string,total_bytes);
  26.  
  27.                     //find_and_replace(du_string, "\n", "");
  28.  
  29.                     string cmd_server_status_mysql = (total_bytes);
  30.  
  31.                 printf(cmd_server_status_mysql.c_str());
  32.  
  33.                 }
  34.  
  35.             pclose(pFile);
Thank you..
Jan 27 '08 #1
3 2669
hsn
237 New Member
if you have a string str="welcome to thescripts\nc++,c";
and another string str2;
if you used the function getline(str,str2) str2 will be "welcome to the scripts" (not including the '\n'.
if you want to use cin in the getline function you just put it in the first parameter.

good luck and i hope i was helpful
Jan 27 '08 #2
hsn
237 New Member
Hello,

I am having trouble getting the getline() function in C++ to work I ahve read the references but I'm stilling having a hard time trying to get it to work can anybody help? thanks!

Expand|Select|Wrap|Line Numbers
  1.             FILE * pFile;
  2.                char du_sum[50];
  3.  
  4.             string check_dir = ("servers/" + cmd_svrid);
  5.             string du_dir = ("du -sb " + check_dir);
  6.  
  7.             pFile = popen(du_dir.c_str(), "r");
  8.  
  9.                 if (pFile == NULL) {
  10.  
  11.                     perror ("Error opening file");
  12.  
  13.                 } else {
  14.  
  15.                     fgets(du_sum, 100, pFile);
  16.  
  17.                     string du_string = du_sum;
  18.  
  19.                     find_and_replace(du_string, check_dir, "");
  20.  
  21.                     whitespace(du_string);
  22.  
  23.                     string total_bytes;
  24.  
  25.                     getline(du_string,total_bytes);
  26.  
  27.                     //find_and_replace(du_string, "\n", "");
  28.  
  29.                     string cmd_server_status_mysql = (total_bytes);
  30.  
  31.                 printf(cmd_server_status_mysql.c_str());
  32.  
  33.                 }
  34.  
  35.             pclose(pFile);
Thank you..
try to use getline without the functions before it will it work or not????
Jan 27 '08 #3
Ganon11
3,652 Recognized Expert Specialist
If you are using C++, why are you using FILE* and legacy C functions? Try ifstreams and ofstreams, which will act live cin and cout with files. Much easier to work with.
Jan 27 '08 #4

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

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.