473,811 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you make new lines in text files?

compman9902
105 New Member
How Do You Make New Lines In Text Files? Just A Quick Question In Need Of A Quick Answer. Keep In Mind That The Character(s) That Are Needed Have To Be Stored In A String Variable.
Thank You.

-Parker Woods
Mar 23 '07 #1
15 2718
dmjpro
2,476 Top Contributor
"\n" is not working ???????
Mar 23 '07 #2
compman9902
105 New Member
"\n" is not working ???????
Here is the code that is being used:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. string myString;
  3. string text;
  4. if (text == "\n")
  5. {
  6. myString = myString + "\n"
  7. }
  8. ...
  9.  
After that, it just goes on putting it into the text file as one big string. I know that works because every part of the string gets put in up until the new line.
Mar 23 '07 #3
compman9902
105 New Member
Here is the code that is being used:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. string myString;
  3. string text;
  4. if (text == "\n")
  5. {
  6. myString = myString + "\n"
  7. }
  8. ...
  9.  
After that, it just goes on putting it into the text file as one big string. I know that works because every part of the string gets put in up until the new line.
It just occured to me...When you are getting input from a text file, does it read the "\n", or is it discarded? Hmmm...
Mar 23 '07 #4
Xaro
1 New Member
Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString + "\n"
  6. }
  7.  
Hmm, let me think now...

Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString
  6. std::cout << "\n"
  7. }
  8.  
That should work.
Mar 23 '07 #5
compman9902
105 New Member
Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString + "\n"
  6. }
  7.  
Hmm, let me think now...

Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString
  6. std::cout << "\n"
  7. }
  8.  
That should work.
Nope, that dosn't work.
Please remember that the newline has to be in a string and that string will be put into a text file.
Mar 23 '07 #6
DeMan
1,806 Top Contributor
Where are you getting "text" from (I realise the declaration is immediately before the if here just to highlight how it was declared), but I wondered whether you can post where you give text a value
Mar 23 '07 #7
compman9902
105 New Member
Where are you getting "text" from (I realise the declaration is immediately before the if here just to highlight how it was declared), but I wondered whether you can post where you give text a value
I'm getting text from another text file. You see, I'm makeing a decrypter and the encrypter actualy gives every new line a numarical value, so thats whats happening.
Mar 23 '07 #8
DeMan
1,806 Top Contributor
Sorry, what I meant is what is the command you are using to fill the variable "text".
In the example you declare it and immediately test it (without assigning it a value). I understand this is for demonstration purposes, but how are you reading a value into "text" in your program.....

Assuming you read it line by line using getline, then you don't even need to test for "\n", simply add a "\n" after every call to getline().
Mar 23 '07 #9
compman9902
105 New Member
Sorry, what I meant is what is the command you are using to fill the variable "text".
In the example you declare it and immediately test it (without assigning it a value). I understand this is for demonstration purposes, but how are you reading a value into "text" in your program.....

Assuming you read it line by line using getline, then you don't even need to test for "\n", simply add a "\n" after every call to getline().
Oh, sorry for the misunderstandin g. Well here is what I'm using:
Expand|Select|Wrap|Line Numbers
  1. string fileOpen;
  2. cin  >> fileOpen; //The Path Of The File To Be Opened
  3. ifstream fileopen;
  4. fileopen.open(fileOpen.c_str());
  5. if (fileopen.is_open())
  6. {
  7. while (! fileopen.eof() )
  8. {
  9. getline (fileopen,line);
  10. text = text + line;
  11. }
  12. fileopen.close();
  13. }
  14. else
  15. {
  16. cout << "The File Was Unable To Open For An Unknown Reason. Please Check The Name" << endl;
  17. cout << "You Gave The File And Try Again." << endl;
  18. cout << "                  Program Terminated" << endl;
  19. system("PAUSE");
  20. return 0;
  21. }
That is what I'm using to fill in "text".
But I will try to do that, you know, just add a "\n"

--Parker Woods
Mar 23 '07 #10

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

Similar topics

14
7456
by: nic977 | last post by:
I am asked to write a simple program to displays the last n lines from a given text file. But I have no ideas how C defines a "line" in a text file. How does it tell if it is the end of the line, is there such thing call EOL like the EOF? -- Posted via http://dbforums.com
1
7712
by: a | last post by:
I'm trying to remove specific lines from text files. I need to always remove lines 1 thru 8 and lines 10 and 11 from text files. Seems like a simple task, but I'm not getting it. I'm reading the file via webresponse as a stream and then trying to remove the lines before parsing it and then writing it to an sql table. Any ideas on how I can accomplish the line removal/deletion?
9
3191
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I want to be able to draw lines in a picturebox based upon certain data points I have received. I dragged a picturebox from the toolbar onto my form, but after having gone through the help files, looking online and trying a variety of things, I...
5
1515
by: Justin Fancy | last post by:
Hi everyone, I need some help. I'm placing text files into a created database using vb.Net. The problem is that, i need two seperate sql statements to add both files because they are in different loops. My output comes out to be as Follows: TABLE ---------------------------------------------------- Field1 Field2
42
6833
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." My question is that if it stops at a new line character (LF?) then how does one read a file with...
6
24159
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would like to split it and create a new file at every lets say 20000 lines... so, the directory output would have to be something like this:
7
2120
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
3
2943
by: ckirchho | last post by:
Hallo, my task is to establish some kind of full text file search in Javascript. The files and it's contents are fixed, the HTML/JavaScript pages lie offline on a server. As far as Javascript cannot access files via any i/o operations, I have written a routine in Delphi. It parses files in one folder and generates a Javascrip tfile. The first lines build up a file table, then the word index is build.
9
13240
by: NvrBst | last post by:
Whats the best way to count the lines? I'm using the following code at the moment: public long GetNumberOfLines(string fileName) { int buffSize = 65536; int streamSize = 65536; long numOfLines = 0; byte bArr = new byte;
2
4103
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
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
10662
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
10416
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
10138
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...
0
5567
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
5702
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
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 we have to send another system
3
3028
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.