473,799 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question of reading files with buffer limit

2 New Member
Hey guys,

I am working on a program which is supposed to read input from a file at 100 chars per time, then change the spaces to underscores and reverse the order of the entire document. Now we are not allowed to go over 100 input characters per time. I had the project finished until i realized that after 100 characters the program would print the incorrect part first.

for (i=0;i<n;i++)
{
if(buffer[i-1]== 32)
buffer[i-1]=95;
tempArray[n-i]=buffer[i-1];
flag=1;

}
this correctly changes the spaces to underscores and reverses the order of which they are inputted.

Here is my completely edited program which gets a segmentation error and I cannot get around it.

By the way it takes 3 files as input (which i am sure I dont need) and currently doesnt output anything right. I was attemping on using a dynamic array to fix this but I believe it keeps overwriting itself.

Any help I would be greatful.

-E




#include <stdio.h>
#include <sys/file.h>
#include <string.h>

int main(int argc, char* argv[])
{
int readFile, writeFile, intTemp, i, x, n, flag=0, count=100, factor=0,
times=0;
char buffer[100];
char tempArray[100];
char *stackIt;
char FILENAME[20] = "tempFile.t xt";

int getSize(char *A)
{
return sizeof(A)/sizeof(char);

}


if (argc < 3)
{
printf("Error: An input and an output file name must be
provided!");
exit(0);
}

if (!strcmp(argv[1], argv[2]))
{
printf("Error: The files must have distinct names!");
exit(0);
}

readFile = open(argv[1], O_RDONLY);
if (readFile == -1)
{
printf("Error opening input file.");
exit(0);
}

writeFile = open(argv[2], O_CREAT | O_RDWR | O_APPEND, 0644);
int tempFile = open(argv[3], O_CREAT | O_RDWR | O_APPEND, 0644);

if (writeFile == -1)
{

printf("Error opening output file.");
exit(0);
}

n = read(readFile, buffer, 100);
while (n != 0)
{
if(flag==1)
{
for (i=0;i<n;i++)
{
stackIt[times] = tempArray[i];
times=times+1;
}
}
for (i=0;i<n;i++)
{
if(buffer[i-1]== 32)
buffer[i-1]=95;
tempArray[n-i]=buffer[i-1];
flag=1;

}
n = read(readFile, buffer, 100);
}

n = getSize(stackIt );

while(n != 0)
{
x = write(writeFile , stackIt, 100);
n=n-x;
}

n = write(1, "Program completed!\n", 19);

close(readFile) ;
close(writeFile );

exit(7);
}
May 1 '07 #1
3 2428
JosAH
11,448 Recognized Expert MVP
Are you allowed to use random file access? If so read the last chunk, process
and reverse it and write it out to the output file. Read the previous chunk etc. etc.
btw, comparing filenames doesn't buy you much i.e. "./foo" and "foo" both
represent the same file.

kind regards,

Jos
May 1 '07 #2
scorro1
2 New Member
I am not sure how to read the last chunk first. Any ideas how to start it?
May 1 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
I am not sure how to read the last chunk first. Any ideas how to start it?
Since you are using the unbuffered IO functions, you would require a call to lseek() to move the file descriptor to the correct position.

Good luck.


Adrian
May 1 '07 #4

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

Similar topics

7
2393
by: Stephen Boulet | last post by:
First of all, I'm not sure that the easiest way to do this is with python ... I'd like to set up a web page that would accept a text file, process it with a local program, and then make available generated pdf and postscript files for downloading. I'd like to limit the size of the file to be uploaded. I have apache2 already up. Is this a job for (mod_)python?
0
1925
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work okay, but reading from it results in EBADF. It also causes python to crash on exit. I've attached the minimal (I think) c code, python code, build script, build log, and run log. Any and all help is greatly appreciated.
9
3418
by: R.Neeser | last post by:
Hello, how do i get an keyboard input from the consol, WITH THE Space char? scnaf and all the other function divide the input string on such a char and give every part to a different variable. but i don't need that. char buf; scanf("%s",char); printf("%s",buf);
19
10382
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
3
2085
by: Brad | last post by:
I'm working on a web app which will display LARGE tiff image files (e.g files 10-20+ mb). Files are hidden from users direct access. For other, smaller image files I have used FileStream to read in a file in a single Read and so my quesitons are: (1) What is a practical file size limit for reading using FileStream.Read (reading the file in a single read)...especially on a web server where I don't think I'd want to tax memory...
1
2207
by: Etienne Desautels | last post by:
Hi, I'm working on project where I need to grab video from an Axis IP camera. This camera send a stream of a multipart message on HTTP. I write this code (at the bottom of the message) to read and uncompress each jpeg images. I call nextFrame() 24 times per seconds (or every 0,0416 sec.) to read the next image that arrived. Everything works well but one thing. My problem is that running nextFrame() took, most of the time, more then...
21
6397
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing this (it's only a test trying to achieve the goal...):
7
2635
by: Vlad Dogaru | last post by:
Hello, I suspect this comes up quite often, but I haven't found an exact solution in the FAQ. I have to read and parse a file with arbitrarily long lines and have come up with the following plan: 1. start with a statically allocated buffer and a pointer of equal size 2. read into the buffer using fgets and append to the pointer 3. if buffer does not contain '\n', reallocate buffer and jump to 2 4. return the pointer
10
19372
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see that memory allocation and the process w3wp is running. After some time (more or less 2 minutes) I get a response timeout. Here is the code:
0
9686
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
9540
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,...
1
10222
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
10026
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
9068
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...
0
5463
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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
2938
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.