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

Question of reading files with buffer limit

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.txt";

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 2405
JosAH
11,448 Expert 8TB
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
I am not sure how to read the last chunk first. Any ideas how to start it?
May 1 '07 #3
AdrianH
1,251 Expert 1GB
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
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...
0
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...
9
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....
19
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...
3
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...
1
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...
21
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...
7
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...
10
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.