1

I am working on a school assignment and I need a little bit of help.

My question is, how can I compose characters that are read in from a file into a single string of memory. Here is what i have so far.

My Code:

#include <stdio.h>
#include <stdlib.h>
char myString;

int main(int argc, char *argv[]){
    FILE* fin;
    char ch;

    fin=fopen(argv[1],"r");
    while((ch=fgetc(fin))!=EOF){
            printf("%c\n", ch);
    }
    fclose(fin);
    return 0;
}

My teacher said the last part of main is to be:

   putchar(‘\n’);    
   printf( myString );
   return 0;
}

But I'm not sure how to put that within my code. Thank you ahead of time, Im also not looking to be just given the answer if you could help me work it out that would be great thank you again.

Updated code:

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]){
    FILE* fin;
    int i;
    char myString[3];

    fin=fopen(argv[1],"r");
    while(fgets(myString,sizeof(myString), fin)){

            putchar('\n');
            printf("%c\n", myString[i]);
    }
    fclose(fin);
    return 0;
}

Im unsure if this code is exactly correct. It prints out the items within the file and puts a space between them. and there is an array being used for the string.

Cka91405
  • 165
  • 1
  • 8
  • 17

4 Answers4

4

Im also not looking to be just given the answer

Fine.

  • Define a char array (mystring) large enough to hold your string
  • Define a counter to keep track of the position in the array
  • At each step store ch into the array
  • Remember to 0-terminate it (store 0 as the last element).

Things to note:

  • You will need to learn about realloc and grow the storage as you go if your program is to read arbitrarily long input. Better leave that for later
  • It's generally unsafe to printf(dynamicstr). What if it contains a "%d" ? It's better to printf("%s", dynamicstr).
cnicutar
  • 178,505
  • 25
  • 365
  • 392
0

You get to learn about memcpy and malloc. They are your friends.

--Jason

jdouglas71
  • 21
  • 3
  • While I agree `memcpy` is tremendously useful, I don't see how it can help in this case ? – cnicutar Nov 25 '11 at 20:02
  • @BrianRoach I thought `realloc` is a better way to do it than `malloc` + `memcpy`. But you do have a point :-) – cnicutar Nov 25 '11 at 20:05
  • @cnicutar- Yeah ... I thought about that and actually deleted my comment, but then you also have that whole "When realloc fails" etc, etc ... but again, *way* past where the OP is. – Brian Roach Nov 25 '11 at 20:07
0

You need to read the applicable part of your textbook that explains what a "string" is in C. Without you understanding that, there's almost no way to answer your question without simply doing it for you.

A C "string" is a contiguous chunk of memory containing chars and is NULL terminated. You'll need to allocate that then place the characters into it as you read them from the file. Note that you have to make sure you don't go beyond the memory you've allocated while doing so, this is called a "buffer overflow".

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • So if i use malloc to allocate space for the array would that be a better way of getting it to work. – Cka91405 Nov 25 '11 at 20:14
  • @Cka91405 - Correct. You need to `malloc` a suitable initial buffer based on what you know about the file - basically a good guess at a buffer size. As you copy `char`s into it you need to keep track of how much of that buffer has been used and not overflow it. If you fill the buffer you'll need to `realloc` – Brian Roach Nov 25 '11 at 20:17
  • Okay thank you for your help. I think im understanding a little bit more. Im going to continue reading about malloc and creating a buffer. Thanks again – Cka91405 Nov 25 '11 at 20:40
0
#include <stdio.h>
#include <stdlib.h>

char* myString;
int main(int argc, char *argv[]){
    FILE* fin;
    fpos_t fsize = 0;
    char ch;
    char *pch;

    fin=fopen(argv[1],"r");
    fseek(fin,0,SEEK_END);
    fgetpos(fin, &fsize);
    rewind(fin);
    pch = myString = (char*)malloc((fsize+1)*sizeof(char));
    while((ch=fgetc(fin))!=EOF){
            *pch++ = (char)ch;
    }
    *pch = '\0';
    fclose(fin);
    printf("%s", myString);//printf(myString);// need escape %
    free(myString);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thank you that was helpful. But I just realized i cant use C’s string handling functions. That i need to create an array of the correct length and type, constructing a real string “by hand.” ..How would i do that any suggestions? Thanks – Cka91405 Nov 26 '11 at 18:33
  • I'm sorry if you misunderstood, "read in from a file into a single string of memory." then , you need the size of the file to know the amount of memory allocated. If you can not use a library function, it must create a function to query the number of characters in a file read all the files in advance. – BLUEPIXY Nov 26 '11 at 22:08
  • I understand what you are saying. I was just adding that I didn't see one part to the assignment, but ill figure it out thanks. – Cka91405 Nov 27 '11 at 05:32
  • @Cka91405, What? Here is a fine. – BLUEPIXY Nov 27 '11 at 23:34
  • it wouldnt work when i input everything not to copy just to get the feel for how your code works and how i could incorporate information to create mine. im still working on it and im stuck :( – Cka91405 Nov 28 '11 at 01:44