我如何获得输入切断或在某一点环绕? [英] How do I get the input to cut off or wrap around at a certain point?

查看:137
本文介绍了我如何获得输入切断或在某一点环绕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alrighty,所以一天一点正对计算器后,我才知道它是在这个网站:)我最终得到我的程序的工作是非常有用的。我能得到的文本文件的无限量在命令行上,并显示他们的!所以它看起来是这样的:


CMD控制台

C:\\ Users \\用户名\\桌面> wrapfile.exe hello.txt的how.txt。 are.txt you.txt random.txt

您好你今天做什么?我希望你做得很好。这只是一个测试,看看多少我可以在屏幕上显示。


现在,我瓦纳建立在这一计划。我该如何获得这个新发现的文字环绕?就像,如果你想让它说,每40个字符左右,文本跳转到下一行......我们怎么会去这样做类似的东西?

再次感谢!

这里的code我的工作:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
INT主(INT ARGC,字符** argv的)
{    INT L = 1;
        而(L!= ARGC)
{
        FILE * FP; //声明变量
        FP = FOPEN(的argv [L],RB);
        升++;
    如果(FP!= NULL)//从FOPEN检查返回值
    {
        INT I = 1;
        做
        {
            I =龟etc(FP); //扫描该文件
            的printf(%C,我);
            的printf();
        }
        而(ⅰ= - 1!);
        FCLOSE(FP);
    }
    其他
    {
        的printf(错误\\ n);
    }
}
}


解决方案

好了,我们开始吧...这看起来你有点不同,但这是ISO / ANSI C标准的1989年

  INT主(INT ARGC,字符** argv的)
{
     FILE * FD = NULL;
     焦炭linebuf [40];
     INT ARG = 1;     而(ARG< ARGC){
         FD = FOPEN(的argv [参数],R);
         如果(NULL!= FD){
              / *与fgets(的char * buf中,为size_t buflen,FILE * FD):对错误返回NULL。 * /
              而(NULL!=与​​fgets(linebuf,sizeof的(linebuf),FD)){
                  的printf(%S \\ n,l​​inebuf);
              }
              fclose函数(FD);
         }其他{
              fprintf中(标准错误,无法打开\\%s \\的\\ n,argv的[参数]);
         }
         ++ ARG;
     }
 }

Alrighty, so after a day and a bit of being on stackoverflow, I learned it's useful being on this site :) I ended up getting my program to work. I can get an unlimited amount of text files in on the command line and display them as well! So it looks like this:


CMD Console

c:\Users\Username\Desktop> wrapfile.exe hello.txt how.txt. are.txt you.txt random.txt

Hello How are you doing today? I hope you're doing quite well. This is just a test to see how much I can fit on the screen.


Now, I wana build on this program. How would I get this new found text to wrap around? Like, if you wanted to make it that, every 40 characters or so, the text jumps to the next line... how could we go about doing something like that?

Thanks again!

Here's the code I'm working with:

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


int main(int argc, char **argv)
{

    int l = 1;
        while(l != argc)
{
        FILE *fp; // declaring variable


        fp = fopen(argv[l], "rb");
        l++;


    if (fp != NULL) // checks the return value from fopen
    {
        int i = 1;
        do
        {
            i = fgetc(fp);     // scans the file 
            printf("%c",i);
            printf(" ");
        }
        while(i!=-1);
        fclose(fp);
    }
    else
    {
        printf("Error.\n");
    }
}


}

解决方案

Okay, here we go...this looks a little different to yours, but this is ISO/ANSI C 1989 standard.

int main(int argc, char **argv)
{
     FILE *fd = NULL;
     char linebuf[40];
     int arg = 1;

     while (arg < argc) {
         fd = fopen(argv[arg], "r");
         if (NULL != fd) {
              /* fgets(char *buf, size_t buflen, FILE *fd): returns NULL on error. */
              while (NULL != fgets(linebuf, sizeof(linebuf), fd)) {
                  printf("%s\n", linebuf);
              }
              fclose(fd);
         } else {
              fprintf(stderr, "Cannot open \"%s\"\n", argv[arg]);
         }
         ++arg;
     }
 }

这篇关于我如何获得输入切断或在某一点环绕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆