passing a string into a function

Tag: c , string , io Author: twolkpix1 Date: 2014-04-23

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?

What DOES it write? Something like 0x049569...?
I writes something like a box with many characters in it.
Please post what it prints in your question
You never store a value in msg, so it contains garbage.
I tried to paste the output here but when I paste it it doesn't actually show up on here. When I look at the fileon the vim editor it shows up as ^A. When I open it on gedt it shows up as garbage

Best Answer

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;
}

comments:

Thank you for the solution
Your welcome..I did not provide the changes. You should go through others answers and donot use gets use fgets instead.
The down voter of this answer should really leave a reason. i.e. poster is fairly new to SO, give him an idea of how to improve the answer.
@jahan - regarding your comment // getchar();why you were using this?. In a console app, getchar() is a good way to pause execution so you can see the output of a printf() before execution collapses the console.
@ryyker thanks for telling me that. You know some people are addicted to down vote here.

Other Answer1

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);

comments:

did you just advise someone to use gets ?
I tired it using gets.It worked but it says it was deprecated. Is there a better way?
@flaco fread is a much more advisable function
@TaylorFlores: Not for text input. fread will read a specified number of bytes, which means it won't stop at the end of a line (when you type Enter). Use fgets.
@KeithThompson thanks, I changed my suggestion to fgets

Other Answer2

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.)

Other Answer3

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.