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

Specifying number of lines to be read

Hi,

I wrote a script to remove a chunk of data ( a matrix) from one file and operate on it. I then wanted to put it back into the original file and replace the old matrix. I was told I could not put data lines back into the old file (the matric is in the middle of other data). So I tried to put it into a new file, and altered my program to also copy the previous data and put it all in the right order. My original file contains 9000 lines of 5 large floatiing point numbers. Is there a way to tell it to copy all these lines in a shorthand way, I can't imagine I need to write out:

fscanf(in, "%f etc...) and

then use printf 9000 times. Can anybody help me with this?

Thanks, Michael
Aug 4 '06 #1
1 1623
Banfa
9,065 Expert Mod 8TB
If you know the position in the original file of the start and end of the data you are reading and writing then you can use

fread and fwrite (you will be reading and writing in binary mode).

In theory you could write the new file in 3 logical steps
  1. Write the beginning of the file
    Call fread to read the entire beginning of the original file in 1 chunk
    Call fwrite to write the beginning of the new file in 1 chunk
  2. Write your new data
  3. Write the end of the file
    Call fread to read the entire end of the original file in 1 chunk
    Call fwrite to write the end of the new file in 1 chunk

You may find that there is a limitation in that you have to supply the buffer for fread and fwrite to read and write into/out of so for a really large file in reality you may have to perform the block read/writes in a number of steps (say use a buffer of 10240 bytes and read and write blocks in multiples of that size) so you may want to create a wrapper function that takes the offset in the original file and the size of the data to copy and then internally if required does the copy in several steps.

When working out offsets and sizes fseek and ftell should come in handy.
Aug 4 '06 #2

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

Similar topics

4
by: Fred Atkinson | last post by:
Is there a function that returns the number of lines in a text file? If so, what is it? Thanks, Fred
3
by: SirPoonga | last post by:
Can I determine how many lines an xml file has so I can say something like "line 4 of 254"?
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
7
by: TheGanjaMan | last post by:
Hi people, I'm stuck on a problem and I was wondering if there was a way around: I'm trying to find the number of lines in a comma delimited text file. I have a progress bar that should...
6
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...
6
by: shana07 | last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc. 3 12 4 64 7 8 10 128 ... I just wanna...
7
by: Anz | last post by:
Is there any javascript function for counting the number of lines inside a text area ?
17
by: SimonPalmer | last post by:
anyone know how I would find out how many rows are in a csv file? I can't find a method which does this on csv.reader. Thanks in advance
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.