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

how to detect end of line \n c++ urgent

hiii
i am trying to read a file which has data in the form of an array .. to count the number of lines i need to count number of \n ... but i m unable to do so ..
char ch;
while(infile>>ch)
{

if(ch=='\n')
cout<<" end ";
else
cout<<" cont. ";

c++;
}
infile.close();
cout<<" c="<<c;
it does not detect \n ...
please help .
i am pretty new to c++.... please reply as soon as poss.. :)
Jun 15 '07 #1
5 26745
Savage
1,764 Expert 1GB
hiii
i am trying to read a file which has data in the form of an array .. to count the number of lines i need to count number of \n ... but i m unable to do so ..
char ch;
while(infile>>ch)
{

if(ch=='\n')
cout<<" end ";
else
cout<<" cont. ";

c++;
}
infile.close();
cout<<" c="<<c;
it does not detect \n ...
please help .
i am pretty new to c++.... please reply as soon as poss.. :)
That's because of >> operator.This operator breakes extraction from file when it encounters whitespace,use getline instead..

Savage
Jun 15 '07 #2
That's because of >> operator.This operator breakes extraction from file when it encounters whitespace,use getline instead..

Savage
can i get the characters without the spaces .. ?? into an array ..??
Jun 15 '07 #3
can i get the characters without the spaces .. ?? into an array ..??
i am reading the array once using getline .. to get no. of lines . .. then again the total number of characters ( by >>)... then i divide the two to get no. of columns.. then i am reading al the characters in the file into an array using >> .. each time i have to use a differnt stream , otherwise there is failure in opening the file ..
is dere a simpler way out..???
Jun 15 '07 #4
You will need two streams to read and write to the file, if memory serves correctly. However, you could use a string to store every line you read using getline and then use a length function to find the length of the string (# of characters). That eliminates one stream. Hope that helps.
Jun 15 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
You may be making this too hard.

1) the file has the strings
2) you need to read the stirngs into an array
3) this is C++. The array is implemented by vector
Ergo: read the strings into a vector.

Expand|Select|Wrap|Line Numbers
  1. ifstream input("data.txt");
  2. char buffer[256];
  3. string str;
  4. vector<string> array;
  5. while (!input.eof())
  6. {
  7.     input.getline(buffer, 256);        
  8.     str = buffer;
  9.     array.push_back(str);
  10. }
  11.  
Jun 15 '07 #6

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

Similar topics

1
by: Mamatha | last post by:
Hi, I have a video capturing with motion detection application in C#.NET.In that application i detected all resources i.e video source and audion source thourgh motion detection,now i want to...
5
by: TulasiKumar | last post by:
Hi all, I am new in Netwrok porgraming in cSharp.how can i detect Tcp/Ip packet in c#.Net.Any body knows please tell me, what are the interfaces or classes are supported regrading on TCP/IP...
2
by: rkgarimella9 | last post by:
Friends, i'm facing with a severe problem while coding an windows application. the problem is that i'm using a usercontrol consisting of three labels upon which three images are placed. the problem...
9
by: erickwan88 | last post by:
I am doing a final year project for my school and is going to provide for an organization, so I am asking for some help on here. Indeed, I have no idea on how to get the input from my pen driver...
5
by: Tom Rahav | last post by:
Hello, How can I create an application/win-service in .NET that runs in the background and detects automatically when the computer connects to the internet? I want to have a program that runs...
1
by: juliacjy | last post by:
Dear all, I'm needing an urgent help. I'm now maintaining a system which has a function of "Automatic Calculation". User is allowed to set of a time which then will be stored in one of my sql...
6
by: Steve | last post by:
Hi All I have an on-screen keyboard within a POS program I have written in VB.net 2005, for touch screen computers I have it set to 'always on top' so the user can move the cursor to...
1
by: Nalaka | last post by:
Hi, We are sending SMS messages using our ASDP.net site (C# code).... How do I add a "Line Feed" character to the string that we are sending? Any help is deeply appreciated Thanks Nalaka
1
by: Steve | last post by:
Hi All I am using vb.net 2005 in a windows forms application I send data to the selected windows printer using a PrintDocument object Is there any way to detect if the Printer is not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.