473,466 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

read last character from a file

33 New Member
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.
Jun 15 '07 #1
4 7313
weaknessforcats
9,208 Recognized Expert Moderator Expert
You should be able to use getline():
Expand|Select|Wrap|Line Numbers
  1. ifstream input("data.txt");
  2. char buffer[256];
  3. input.getline(buffer, 256);
  4.  
getline() converts the \n to a null terminator in buffer so you don't have to worry about it.
Jun 15 '07 #2
Rasputin
33 New Member
You should be able to use getline():
Expand|Select|Wrap|Line Numbers
  1. ifstream input("data.txt");
  2. char buffer[256];
  3. input.getline(buffer, 256);
  4.  
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
Expand|Select|Wrap|Line Numbers
  1. fileInput >> temp;
  2. vecFileContent.push_back(temp); 
  3.  
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.
Jun 18 '07 #3
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
Expand|Select|Wrap|Line Numbers
  1. fileInput >> temp;
  2. vecFileContent.push_back(temp); 
  3.  
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
Jun 18 '07 #4
Rasputin
33 New Member
You could read a whole line by using getline and then tokenize it and push it into vector.

Savage

got it, thanks.

Ras.
Jun 19 '07 #5

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

Similar topics

6
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
17
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.
5
by: Stanley Yue | last post by:
I made a map like this: typedef struct { char * Source; bool DirFlag; } RegInfo_t; struct Comp {
13
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...
2
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;
5
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...
2
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...
35
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",...
2
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...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
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,...
0
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...
0
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...

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.