Click to See Complete Forum and Search --> : Urgent C help needed!!!
RAGEAngel9
09-05-2001, 12:05 AM
Ok I;m writing a program for my Advanced C class and I can't for the life of me remember how to read text from afile one line at a time.
I feel stupid because I should know how to but I jsut can't remember AAAUUUGGGGG!!!
anyway please help me.
Thankx
jemfinch
09-05-2001, 12:08 AM
Standard C has no function for what you want. You'll have to read character by character, storing the characters in a buffer, until you get to a newline character (a carriage return followed by a newline on windows; a carriage return on macos) and then return your buffer.
Jeremy
klamath
09-05-2001, 12:12 AM
Maybe fgets?
C is a really bad application programming language, although I'm learning to tolerate it.
RAGEAngel9
09-05-2001, 12:15 AM
UGG stupid C
ok well I have a text file with 4 things on each line
string int int int
I know how to fscanf each of these out of every line but I'm not sure how to make sure I'm fscanf ing from the next line each time.
grr
Well if anyone can help me I'd appreciate it, otherwise back to searching through mybooks for help.
Later
debiandude
09-05-2001, 12:17 AM
Yes C does have such a function! You want fgets. fgets reads until it sees a new line character or until the length has been reached.
Protype:
char *fgets(char *str, int length, FILE *fp);
#include <stdio.h>
#define MAXSTRING 256
void main(void) {
FILE *fp;
char str[MAXSTRING+1];
int count = 1;
if((fp = fopen("filename", "r")) == NULL) {
perror("ERROR: Can't open file");
exit(1);
}
while(!feof(fp)) {
fgets(str, MAXSTRING, fp);
fprintf(stderr, "%d %s", count, str);
count++
}
}
BTW C is a great language, not one that is just tolerable! :-)
[ 05 September 2001: Message edited by: debiandude ]
[ 05 September 2001: Message edited by: debiandude ]
RAGEAngel9
09-05-2001, 12:27 AM
Thankx for all the quick help guys
ok so Debaindude do u know if my idea here will work?
while(!feof(filename))
{
fscanf(all my crap);
sum = a + b + sum;
}
will this scan the stuff from each line and then goto the next line for the next time through?
RAGEAngel9
09-05-2001, 12:30 AM
man i feel dumb I just went back through my teachers online notes and I think I found what i needed
but I really appreciate your help.
I'll post my info http://shay.ecn.purdue.edu/~ee264/lecture/Lecture-04/img10.htm
debiandude
09-05-2001, 12:33 AM
Okay this is what I would do.
while(!feof(fp)) {
fgets(tmp, MAXLENGTH, fp);
sscanf(tmp, "%s %d %d %d", str, &int1, &int2, &int3);
}
RAGEAngel9
09-05-2001, 12:35 AM
Thankx I'll try that but one question if
the strings i'm reading in have spaces such as
St. something
will that effect the read because I thought it cut at the spaces. If so then what about using
fscanf(in," %[not sure what goes here]", %s);
debiandude
09-05-2001, 12:37 AM
Personally I don't think that that was best example of doing what you want (the link you gave), anyway that snipit I gave you works, it will read in a line and split it into the string, int int int, like you wanted. Don't feal stupid, I happens to us all, I find myself frequenting a reference book every now and then.
debiandude
09-05-2001, 12:39 AM
I am not sure off the top of my head... lemme check
debiandude
09-05-2001, 12:50 AM
Once it hits a " " it is considered a new string. Sorry :-(
RAGEAngel9
09-05-2001, 12:50 AM
Thankx again for helping
RAGEAngel9
09-05-2001, 12:55 AM
Thats what i thoguht so I have to try it this way:
fscanf(in, "%[something]", %n);
the onyl problem is I'm not sure what to throw in there.
RAGEAngel9
09-05-2001, 12:58 AM
Hell this is frustrating to explain so here is my code.
Now of course I neither except nor want anyone to flat out fix it for me I jsut would like to knwo how to read scan one line at a time out of my input file.
int Compute_State_Population(char *filename);
{
FILE *input1 = NULL;
int existcheck = 0;
int readcheck = 0;
int county = 0;
int men_num = 0;
int women_num = 0;
int area = 0;
existcheck = access(filename, F_OK);
readcheck = access(filename, R_OK);
if ( existcheck != 0)
return( CANNOT_OPEN_FOR_READ);
if ( readcheck != 0)
return( CANNOT_READ);
input1 = fopen("filename", "r");
fscanf(input1, "%[a-zA-Z] %d %d %d \n", &county, &area, &men_num, &women_num);
while(feof(input1) == 0)
{
fscanf(input1, "%[a-zA-Z] %d %d %d \n", &county, &area, &men_num, &women_num);
sum = sum + men_num + women_num;
}
return(sum);
}
debiandude
09-05-2001, 01:07 AM
This should do it :-) This code reads a file, then takes it line by line, splits it into string broken up by spaces. Then it puts the string back together complete with spaces and assigns it the tmp variable, then it takes the remaining string which should be ints and converts them into nums in a b c and respectivly. I have to goto bed sorry, but I really do hope this helps.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXELEMENTS 10
#define MAXSTRING 256
char **split(char *delim, char *str);
int main(void) {
FILE *fp;
char tmp[MAXSTRING], str[MAXSTRING], **list;
int i, a, b, c;
if((fp = fopen("filename", "r")) == NULL) {
perror("ERROR: Can't open file");
exit(1);
}
while(!feof(fp)) {
fgets(str, MAXSTRING, fp);
list = split(" ", str);
for(i = 0; i<MAXELEMENTS; i++) {
if(isalpha(list[i][0])) {
if(tmp == NULL) {
sprintf(tmp, "%s", list[i]);
} else {
sprintf(tmp, "%s %s", tmp, list[i]);
}
} else {
a = atoi(list[i]);
b = atoi(list[i+1]);
c = atoi(list[i+2]);
break;
}
}
fprintf(stderr, "%s %d %d %d", tmp, a, b, c);
}
return 0;
}
char **split(char *delim, char *str) {
char *token;
static char *list[MAXELEMENTS];
int i = 0;
token = strtok(str, delim);
list[i++] = token;
while(((token=strtok(NULL, delim)) != NULL) && (i<MAXELEMENTS)) {
list[i++] = token;
}
return(list);
}
[ 05 September 2001: Message edited by: debiandude ]
RAGEAngel9
09-05-2001, 01:14 AM
WOW
thats extensive
but I don't think I'm a loud to use that because it has extra #define 's which aren't aloud (ys it sux but ..)
I really appreciate all that effort though.
As crap as it look the way in that link I showed you is I think the way I need to go. The only problem is what do I put in the brackets.
Since I need to be able to scan in letters, periods, and spaces I think I should use
fscanf(input1, "%[A-Za-z. ]", %s);
but I'm not sure if thats correct
debiandude
09-05-2001, 01:34 AM
The defines can be gotten rid of pretty easily, just replace them ;-) As for using scansets, I really am not sure what the correct way woudl be... THe only thing I know is the above code using a file called filename with
asfg bfda asfdiasfd 123 432 91230
worked for me. Crap I really need to hit the sak for real this time. Once again good luck.
RAGEAngel9
09-05-2001, 01:36 AM
Thankx a hell of a lot man
And i mean it
but anyway good night
hopefully my TA's will actually be helpful (yeah right)
at least its not due until 5 tomarrow.
oh well night night everyone.
TheLinuxDuck
09-05-2001, 03:39 PM
RAGE,
Just for a different approach, here is my solve. (^=
#include <stdio.h>
#include <stdlib.h>
void parseText(char *buffer);
int main(void)
{
FILE *fptr;
char buffer[1024];
fptr=fopen("file.txt","rt");
if(fptr==NULL) {
perror("Cannot open file.txt");
return 1;
}
while(!feof(fptr)) {
if(fgets(buffer,1024,fptr)!=NULL) {
parseText(buffer);
}
}
fclose(fptr);
return 0;
}
void parseText(char *buffer)
{
char *tempPtr; // used to define where we're at in buffer
char count=3; // i.e. 3 numbers at the end of the string, from file
tempPtr=buffer; // start at beginning of buffer
while(*(++tempPtr)!='\0'); // fastforward to end of buffer
if(*tempPtr=='\n') *(tempPtr--)='\0'; // remove newline, if it exists
while(count>0) { // while we don't have all 3 numbers
if(*tempPtr==' ') { // space, as delimiter
printf("Number %d: %d\n", count--, atoi(tempPtr+1)); // show number
*tempPtr='\0'; // truncate buffer to remove number
}
if((--tempPtr)==buffer) return; // so we don't core dump (from line
} // in file not having right format)
printf("String: %s\n", buffer); // all that is left is the string
}