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

How to make a buffer?

Osoascam
Hi! I made this code in order to "create" a buffer...
It works perfectly when reading from console, but it just doesn't work for redinf from files.
(Read after the function for the code explanation)
NAME is a class variable, char*

Expand|Select|Wrap|Line Numbers
  1. void buffer(FILE* entrada, char separador)     //entrada is input (sdtin for consol)
  2. {
  3.     int numChars = 2;                                //# of characters read
  4.     char* aux;
  5.     buff = (char*)malloc(sizeof(char)*numChars);
  6.     NAME = (char*)malloc(sizeof(char)*numChars);
  7.     aux = (char*)malloc(sizeof(char)*numChars);
  8.     fgets(buff,(numChars),entrada);
  9.     *NAME = '\0';
  10.     do
  11.     {
  12.         aux = (char*)malloc(strlen(NAME)+numChars);
  13.         strcpy(aux, NAME);
  14.         free(NAME);
  15.         NAME = (char*)malloc(strlen(aux)*2+numChars);
  16.         strcpy(NAME, aux);
  17.         strncat(NAME, buff, numChars);
  18.         free(buff);
  19.         numChars = numChars*2;
  20.         buff = (char*)malloc(numChars+1);
  21.         fgets(buff,(numChars),entrada);
  22.  
  23.     }
  24.     while (strchr(buff, separador) == NULL);       //if "separador" isnt in the line
  25.     strncat(NAME,buff,strlen(buff));
  26.     NAME[strlen(NAME)-1] = '\0';
  27.     free(aux);
  28.     free(buff);
  29. }
OK, so this is what it does:
Three variables: buff, NAME and aux
For readin' from console, the inputs are buffer(stdin,'\n')
Buff grows double, as well as NAME and aux. Normally, NAME and aux contain the same, but I free NAME and aux form making them bigger.

So, why it doesn't work for files if I just put
buffer(file-name, EOF)
?

Thank you!!
Feb 26 '07 #1
2 13629
Hi,

Your File Pointer entrada, in case of a file ,should specify the mode in which the file is to be opened..

say,
entrada = fopen(FilePath,"r");

where filepath is the actual file with its path.
Feb 26 '07 #2
Thank you, but... It still doesn't work
The problem is it keeps reading forever
Mar 1 '07 #3

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

Similar topics

2
by: Kostadis.Spirou | last post by:
how can i make this programm with class???? #include <fstream> #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std;
5
by: venkatesh | last post by:
hai, I am try to implement when I am clicking mouse key it should perform some actions ,please any one know please tell about how to make it? please tell any links for that operation
18
by: MLH | last post by:
I have an A97 app that automatically sends eMail using OutLook Express. Each installation requires the user to reconfigure Outlook Express to avoid prompts waiting for them to authorize or deny...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
1
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link...
1
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B | last post by:
first, I'm try the POINTER to convesion the pointer type. but failed. class STUDENT(Structure): _fields_ = buffer = c_byte * 1024 student_p = cast(buffer, POINTER(STUDENT)) The parameter...
2
by: Logan Lee | last post by:
BACKGROUND READING: http://www.geocities.com/logan.lee30/a_crude_method_of_static_code_analysis.html http://www.geocities.com/logan.lee30/A_method_of_static_code_analysis.html Like this: ...
3
by: domnulnopcea | last post by:
i have a dinamically generated file! and i have a button on my page... i want to download the file by ajax when i click the button! i tried to send the headers in the xhtmlrequest but my browser...
3
by: PeterDoh | last post by:
im in the creation of an sprite packer for a game, but i can't get it to run? a friend sent me the source for it, so i have no idea what to do from this? so far: import java.io.File; import...
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: 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: 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...
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...

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.