Question

I am trying to pass a string into a function in C. That string will entered by the user and then passed on to the function to write to a text file. Iknow this seems very basic but I am just learning C.

#include <stdio.h>
#include <string.h>

void read() {

        char text[50][30],buffer[150];

        int i=0;
        FILE *file_in;
        file_in=fopen("test.txt","r");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        while (fgets(buffer,150,file_in)) {
                strcpy(text[i],buffer);
                printf("line %d: %s\n",i,text[i]);
                i++;
        }

        getchar();

        fclose(file_in);
}

void write(char str[])
{
        FILE *file_in;
        file_in=fopen("test.txt","a");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        //write to the file
        fprintf(file_in, "\n%s", str);
//      fputs(str, file_in);

        fclose(file_in);
}

int main()
{
        read();

        char msg[50];

        printf("Enter some text: ");
        puts(msg);

        write(msg); 

        return 0;
}

It writes to the file, but it writes weird characters, not what I actually type. What amI doing wrong?

Was it helpful?

Solution 4

Here is the solution:

#include <stdio.h>
#include <string.h>

int read() {

        char text[50][30],buffer[150];

        int i=0;
        FILE *file_in;
        file_in=fopen("test.txt","r");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        while (fgets(buffer,150,file_in)) {
                strcpy(text[i],buffer);
                printf("line %d: %s\n",i,text[i]);
                i++;
        }

       // getchar();why you were using this?

        fclose(file_in);
      //  return 0;
}

void write(char str[])
{
        FILE *file_in;
        file_in=fopen("test.txt","a");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        //write to the file
        fprintf(file_in, "\n%s", str);
//      fputs(str, file_in);

        fclose(file_in);
}

int main()
{
        char msg[50];
        read();



        printf("Enter some text: ");
       // getchar();
        gets(msg);//It reads in msg 

        write(msg); 

        return 0;
}

OTHER TIPS

It looks like you've confused gets with puts. puts writes a string to the console. gets reads a string from the console. Switch them out and your program should work.

Microsoft's compiler often warns against insecure or deprecated functions, like gets. You may use fgets instead, as it doesn't allow buffer overflows.

Here's an example:

fgets(msg, 50, stdin);

or

fgets(msg, sizeof(msg), stdin);

First of all: Don't call your functions read() and write() — pick something more specific. The function names read() and write() are already used by the system for low-level file operations, and trying to define them yourself will cause unexpected behavior.

Second: you are never initializing the contents of the msg variable or reading any data into it, so its contents will be random. (Remember that puts() prints data; it doesn't read anything.)

When you write char msg[50];, it contains indeterminate values. It's not zero-initialized or anything.

The puts(msg); line writes this garbage out, and then write(msg); writes that garbage to a file.

I guess you intended to have some code for inputting text, after the printf and before the puts.

NB. In your read() function (which you don't call yet), you should make the fgets buffer size match the width of your array , and you should check you don't run off the end of your array if the file has many lines.

Also it would be wise to name your functions something other than read and write, because in a POSIX environment there are already functions by that name which may clash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top