Hello,
The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me know for next time.
I am reading strings from a file to a vector, doing some "operations " on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last string is read twice (because I read using while(inputFile .good()){...} ).
So when I am rewriting my new file, I read from the vector and write to a txt file, and since the last string is recorded twice, it is then written twice. I would like to avoid that by checking if the last character of the file is not "\n". Also, since I'm already making a pass reading with strings, I would like to avoid making a second using getc() and check its final value. Avoiding the first pass with strings is not an option.
A temporary solution could be to compare the last 2 strings if they are equal, but this is risky.
I have thought about trying to point to the last character, but I'm lacking knowledge. I saw in the help methods like tell()s and seek()s, but it's not quite clear to me how they can be useful (they return "strange" datatypes).
As always, any thoughts/advices/help would be appreciated,
Ras.
4 7406
You should be able to use getline(): -
ifstream input("data.txt");
-
char buffer[256];
-
input.getline(buffer, 256);
-
getline() converts the \n to a null terminator in buffer so you don't have to worry about it.
You should be able to use getline(): -
ifstream input("data.txt");
-
char buffer[256];
-
input.getline(buffer, 256);
-
getline() converts the \n to a null terminator in buffer so you don't have to worry about it.
Let me put it like this:
I have a multiple lines txt file, with different strings separated with spaces (it could be assimilated to a normal text).
I want to put all these strings in a vector, so I use -
fileInput >> temp;
-
vecFileContent.push_back(temp);
-
I perform some operations on those strings using my vector.
Then, I want to rewrite a new file, so I copy the content of my vector to that file.
Maybe I didn't get you right, but getline() is not helping me out because I still need to use .operator>>() method to fill my vector. The problem occurs because of the \n at the end of the file.
Your time is always appreciated,
Ras.
Savage 1,764
Recognized Expert Top Contributor
Let me put it like this:
I have a multiple lines txt file, with different strings separated with spaces (it could be assimilated to a normal text).
I want to put all these strings in a vector, so I use -
fileInput >> temp;
-
vecFileContent.push_back(temp);
-
I perform some operations on those strings using my vector.
Then, I want to rewrite a new file, so I copy the content of my vector to that file.
Maybe I didn't get you right, but getline() is not helping me out because I still need to use .operator>>() method to fill my vector. The problem occurs because of the \n at the end of the file.
Your time is always appreciated,
Ras.
You could read a whole line by using getline and then tokenize it and push it into vector.
Savage
You could read a whole line by using getline and then tokenize it and push it into vector.
Savage
got it, thanks.
Ras.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: sea |
last post by:
I have text files in the following format:
123,34,
,345,890,
123,23
..
..
..
As you can see, the problem is that (1) the commas can occur in
|
by: Guyon Morée |
last post by:
what is the difference?
if I open a text file in binary (rb) mode, it doesn't matter... the read()
output is the same.
|
by: Stanley Yue |
last post by:
I made a map like this:
typedef struct
{
char * Source;
bool DirFlag;
} RegInfo_t;
struct Comp
{
|
by: Don Kim |
last post by:
I'm writing a program to read 2,000,000 floating point numbers off a text
file, to compute the sum, mean, and median. This is a direct example of
Stroustrup's paper.
But the program will not display the total number of elements beyone 23,272:
#include<vector>
#include<fstream>
#include<iostream>
#include<algorithm>
|
by: Profetas |
last post by:
I have the following code that detects a
<c>
and
</c>
#include <stdio.h>
main(int argc, char *argv)
{
FILE* fp;
char data;
| |
by: Pete |
last post by:
I having a problem reading all characters from a file. What I'm trying to
do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want
to && with a mask 0xFF000000 then >> right shift 24 bits storing in result
then printing the result.
I thing a while or for loop is needed but I'm not quite sure how to go about
it. How do I step through each character in this case and store it for use
and passing to another function.
...
|
by: Stan Sainte-Rose |
last post by:
Hi,
I need to read a kind of text file from vb.net
I do know how to read a simple file, but this one seems to have special
characters.
Some characters appears like squares when open this file with notepad.
When I read the file from vb, I just have the last square.
How to avoid this and remove all the squares ?
Thanks for your help
|
by: RyanS09 |
last post by:
Hello-
I am trying to write a snippet which will open a text file with an
integer on each line. I would like to read the last integer in the
file. I am currently using:
file = fopen("f.txt", "r+");
fseek(file, -2, SEEK_END);
fscanf(file, "%d", &c);
this works fine if the integer is only a single character. When I get
into larger numbers though (e.g. 502) it only reads in the 2. Is there
|
by: Tiger |
last post by:
I try to write a struct into a brut file but I can't write all var in
this struct
when I try to add user I have that :
> testmachine:/usr/share/my_passwd# ./my_passwd -a users.db
> Ajout d'une entrée dans la base :
>
> Username : test
> Password : aze
> Gecos : qsd
|
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...
|
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,...
| |
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |