473,762 Members | 8,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a small text file contents into an array

Hi, another question from me again, i was just wondering if anyone
could give me a quick example of reading data from a file then placing
the data into an array for some manipulation then reading that array
back into another file.

I've tried, but i can only read the data and place it on the screen, i
cant get it into an array.

Any help would be helpful.

Thanxs!
Jul 22 '05 #1
6 5650

"Foxy Kav" <fo*****@hotmai l.com> wrote in message
news:32******** *************** ***@posting.goo gle.com...
Hi, another question from me again, i was just wondering if anyone
could give me a quick example of reading data from a file then placing
the data into an array for some manipulation then reading that array
back into another file.

I've tried, but i can only read the data and place it on the screen, i
cant get it into an array.

Any help would be helpful.

Thanxs!


Really need to know what kind of data you are trying to read, chars,
strings, ints, what?

Also this is much better done with a vector rather than an array, because
then you don't have to worry about the size of the array, dynamically
resizing it etc. It makes the code a lot easier.

john
Jul 22 '05 #2
On 27 Apr 2004 05:07:14 -0700, fo*****@hotmail .com (Foxy Kav) wrote:
Hi, another question from me again, i was just wondering if anyone
could give me a quick example of reading data from a file then placing
the data into an array for some manipulation then reading that array
back into another file.

I've tried, but i can only read the data and place it on the screen, i
cant get it into an array.


If you want the whole file in an array (or vector), you can do:

#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>

int main()
{
//open the file in binary mode (to get raw chars)
std::ifstream ifs("myfile.txt ", std::ios_base:: binary);
//create an iterator pair over the contents of the file
std::istreambuf _iterator begin(ifs), end;
//copy the iterator range into a vector
std::vector<cha r> contents(begin, end);
//close the file
ifs.close();

//change a character - contents contains the whole file here
//be careful to check how big contents is (via contents.size() ).
contents[0] = 'q'; //or contents.at(0) = 'q'; for bounds checking

//open file again for output
std::ofstream ofs("myfile.txt ", std::ios_base:: binary);
//write out whole vector in one go:
ofs.write(&cont ents[0], contents.size() );

//everything cleaned up automatically by destructors -
//one of the joys of C++ over C!
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
Uh .. thanxs for that, but im just a beginner and that makes
absolutely no sence to me.

Im trying to write alphanumeric charcter to a file, so char is the
type of data i want to write to a file, and get from a file.
Jul 22 '05 #4
Foxy Kav wrote:

Uh .. thanxs for that, but im just a beginner and that makes
absolutely no sence to me.

Im trying to write alphanumeric charcter to a file, so char is the
type of data i want to write to a file, and get from a file.


So what is it? Read or Write
In your original question you asked about reading from a file. Can
you write to a file?

What does the file look like?
It is very rare that one has to read from a file character per character,
that's why I ask. Is there some structure in the file?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
On 28 Apr 2004 04:10:52 -0700, fo*****@hotmail .com (Foxy Kav) wrote:
Uh .. thanxs for that, but im just a beginner and that makes
absolutely no sence to me.
Which bits didn't you understand? Or none of it? If you didn't
understand any of it, you'll need to get a good C++ book before going
much further. Have you used ifstream before? vector? They are both
fairly fundamental in C++ (though neither exist in C - perhaps you are
learning from a C book?)
Im trying to write alphanumeric charcter to a file, so char is the
type of data i want to write to a file, and get from a file.


What have you got so far?

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6
On Tue, 27 Apr 2004 13:52:24 +0100, tom_usenet
<to********@hot mail.com> wrote:
std::istreambuf _iterator begin(ifs), end;


That should be:

std::istreambuf _iterator<char> begin(ifs), end;

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1367
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file) My main problem is: getting each paragraph into my vector array - so that each paragraph sits in a new array element. Eg: when i referance array elemant 2 it will give me paragraph which is in that element and not all paragraphs in the text file...
3
7665
by: Markus Hofmann | last post by:
Hi @ all hope someone can help me as my PC is ready to go through the window...here it is: I have a text file with approximately 150,000 lines of the following format: 0007391027,000049-0458-1556-09141999,0023924296, 0007391028,001217-0671-1610-09141999,0023924302,
8
9532
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
2
2496
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
4
7625
by: Andy | last post by:
Hello All: I have a field in the database that is an Image. I have no idea how the data is stored in here (Image, compressed, encrypted, plain text, etc). I am trying to write the contents to a text file, image file, etc so I can see if the data is stored in a way we can understand (we have been tasked to write an app and the app needs to read this field, but we don't know what it really contains). How would I go about reading the...
29
10432
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and then print out the contents of the 2d array to the screen. I wil also need to append data to the .dat file but mostly right now I'm worrying about getting the info into the 2d array. My .dat file looks like this 1 20000 2 30000 3 40000
1
1546
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read everyother line of the file not everyline at first I thought it might be the code then the csv file The csv is a mysql generated via webmin however I cant seem to find the problem anywhere so please advise. -- CODE-- Public Sub csv_conn() 'Opens...
7
11790
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are 'parallel' in the sense that the n-th element of the authors...
4
7903
by: lilyumestar | last post by:
I have project I have to do for class. We have to write 4 different .java files. Project2.java HouseGUI.java House.java HouseSorting.java I already finish House.java and I need to work on both the Project2.java and HouseGUI.java Here are the requirements for those two. The GUI Create a class called HouseGUI which extends JFrame. It should display two text areas (JTextArea) in a grid layout (1 row, 2 columns). Your main program,...
0
9554
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
9378
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
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9927
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
8814
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, 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...
1
7360
isladogs
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...
0
6640
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();...
0
5268
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...
1
3914
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

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.