473,386 Members | 1,752 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,386 software developers and data experts.

read line of string to array of chars

11
I am reading a file line by line I need to copy the line which is string to array of chars named packet.

what function could help me do that.
this is my code:
Expand|Select|Wrap|Line Numbers
  1. char *packet;
  2. packet=new char [1000000];
  3. ifstream  inFile2;
  4. string t, line;
  5. cout<<"Enter a file name.\n";
  6.     cin>>t;
  7.     inFile2.open(f.c_str());
  8.        if ( !inFile2 )
  9.        {
  10.           cout<<"Unable to open file. Enter a different name: ";
  11.           inFile2.clear();
  12.           cin >> t;
  13.           inFile2.open(t.c_str());
  14.  
  15.        }
  16.         while(!inFile2.eof())
  17.         {
  18.  
  19.  
  20.       getline(inFile2, line);
  21.  
  22. //how to copy line to the array packet???
  23.  
  24.  
  25.  
  26.       }
  27.  
  28.  
May 10 '11 #1
6 3421
weaknessforcats
9,208 Expert Mod 8TB
Just create a string object an assign the packet to the object.

Expand|Select|Wrap|Line Numbers
  1. string data;
  2. data= packet;
May 10 '11 #2
ekailan
11
thank you for your reply

I need to copy the string to the array "packet"
May 11 '11 #3
Banfa
9,065 Expert Mod 8TB
If you really need to do that then use std::copy from the algorithms library however weaknessforcats question is why don't you just make packet a std::string then you can simply

Expand|Select|Wrap|Line Numbers
  1. packet = line;
  2.  
Even if you do not want it to be a std::string then it should at the very least be a std::vector rather than a C array in which case you could use std::vector::insert.
May 11 '11 #4
ekailan
11
I cant use any other declaration for the "packet", because its input for another algorithm ...I will try to read a bout "std::copy ".


Thanks
May 11 '11 #5
ekailan
11
I modified the code to this, but I get this error
" out of memory".after three iteration of excution.
Expand|Select|Wrap|Line Numbers
  1. char *packet; 
  2. packet=new char [1000000]; 
  3. ifstream  inFile2; 
  4. string t, line; 
  5. cout<<"Enter a file name.\n"; 
  6.     cin>>t; 
  7.     inFile2.open(t.c_str()); 
  8.        if ( !inFile2 ) 
  9.        { 
  10.           cout<<"Unable to open file. Enter a different name: "; 
  11.           inFile2.clear(); 
  12.           cin >> t; 
  13.           inFile2.open(t.c_str()); 
  14.  
  15.        } 
  16.         while(!inFile2.eof()) 
  17.         { 
  18.  
  19.  
  20.       getline(inFile2, line); 
  21.  
  22. strcpy_s(packet, 500, line.c_str()); 
  23.  
  24.  
May 11 '11 #6
Banfa
9,065 Expert Mod 8TB
If the other algorithm expects a new'd char buffer that it then frees itself this is true (although it speaks of poor design of the other algorithm).

If you just need to pass a char * or const char * and you handle the delete yourself then this is not true. You can use a std::vector which has the same memory footprint as an array, i.e. this is valid

Expand|Select|Wrap|Line Numbers
  1. void function(const char *data);
  2.  
  3. char *pointer = new char[50];
  4.  
  5. std::vector<char> vec(50);
  6.  
  7. function(pointer);
  8. function(&pointer[0]);
  9. function(&vec[0]);
  10.  
Allocating 1000000 bytes to packet seems extremely poor as is copying 500 bytes out of line. All of these sizes can be based on std::string::size to get the actual size required.

"out of memory"

Do you ever free the memory allocated to packet or do you just keep on allocating more? Memory allocation needs to be handled carefully which is why you should try to minimise it where possible or better still hand it off to code that handles it for you like std::vector.
May 11 '11 #7

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

Similar topics

3
by: happy | last post by:
How will I read a string using scanf in that function : to the write what scanedf will be written using fprintf customer_input(cust) /*---------------------*/ struct customer_record *cust; {...
1
by: Jesse | last post by:
I'm having trouble finding a way to read a line from a file until a space or tab occurs. Currently I'm using FileStream* fs = new FileStream(path, FileMode::Open); StreamReader *sr = new...
5
by: RGood | last post by:
Hi, I have a TextBox that contains the following text. User1;User2;User3; ... and so on. Each string is seperated by ";". What is the best way to retreive the strings and place into a string...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
2
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
10
by: mattias.k.nyberg | last post by:
So Im trying to learn to program with C#. And I have this question about why the string array won't work in the first class but it does in the second. To me it looks like they do the exact same...
3
by: Kevin O'Brien | last post by:
Hello, Can someone please tell me how to read a text box in to an array? What I have in the text box is a string of 7 words seperated by spaces. I would like to read each word into an array. ...
12
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
6
by: Studlyami | last post by:
Okay, i have developed a file parser in C++ that i am trying to being into a c# program which is proving to be a lot more difficult than i thought. first i scan a file (which i opened using a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.